Week 11 - Input Devices

Back to electronics this week with input devices. This will be a great opportunity to make a more functional board. We have a selection of input devices in the inventory to choose from. These include a magnetic field sensor, a phototransisor, an acceleometer and so on. This is a good opportunity to make something towards the final project.

Assignment:

Measure something:- add a sensor to a microcontroller board that you've designed and read it


 

9th April 2015 - Designing a practical device

With the choice of inputs avaliable, it will be great to design something that can be used to measure a variable to do with my final project. I asked Neil yesterday whether the Hall effect sensor can be calibrated to read in Tesla (the unit for magnetic fields) and he said it is. All I need to do is look at the data sheet for the device and write the necesary code. This means I can make a generator and really see how the magnets are performing. The strength of a field was the only variable I have been unable to read when looking at calculations for generators.

Looking at the data sheet for the particular component, it appears to have a rather low maximum reading for magnetic fields. Hence I will try to make a scaled down generator, perhaps a tenth of the size of the actual dimensions. But for now I will just get the board up and running!


11th April 2015 - Designing my board

I churned out a design for a board which inclduded the hall effect sensor and the phototransistor. I also attatched 3 rbg LEDs so I would have something to make a simple indicator with. Below is the design for the traces and shape of board:





I ran these through the fab modules and made a silly mistake. I didnt make the design for the shape of the board a solid shape, it was just an outline. This meant that the modules cut two paths. One for the inside of the outline and one for the outside of it. The result of this was a trace being taken off the board which would have to be fixed either by cutting a new board or fixing it with a wire. For now the latter would be better as there could be more issues which may need to be rectified.

So I washed the board and went on to solder the board. This was the result:



After soldering I discovered another problem; the RGB LEDs were connected to the cathode instead of the anode. This would also be an issue. Now that the weekend is upon us, I will re-design the board over the weekend and next week work further on debugging the design.


12th April 2015 - Board re-design

Taking into account the issues I have found so far, I have re-designed the board and will get it milled and soldered. The revised design is below:



My next steps are to get the board made and look at programming it in C code (because that's what the pros use!).

I went through the design with one of our electronics gurus (Emma). She gave it the all clear so now it's time to go and mill the board! While I wait to do that though, I will look at the C programming.

I was able to get my second design milled out and soldered but there were quite a few unseen issues with it.



the first issue was that the reset pin wasn't wired up to the micro controller and instead an RBG LED pin was connected. I left out the connection between the hall effect sensor and the micro controller. Lastly the LEDs I used were the wrong ones for the design. This is a real mess so I will design a third version which will be much simpler.


13th April 2015 - Getting to know C code

To start to get familiar with c-code I went back to the echo board and had a go at uploading the echo code using the tutorial that Neil provided. I managed to get it working and then found some C-code which would get the LED I added to blink with a few adjustments. The code is below:



Now that I had some of my own c code I just had to recall the steps to get it on the board. They are as follows:

Make a folder with the name of the project (I called mine blink)

Inside that folder you need the c code file and the make file. They have the extensions .c and .c.make respectively. Take a look:



I used the make file Neil wrote. All I needed to change was the name of the file to match mine and in the code just replace the project name with the name of the c code file (minus the extension).

Now it is time to go to the terminal. With the ISP and circuit board correctly connected you need to tell the computer to use the .c.make file to convert the c code into something that the micro controller can use and put it on the board. To do this we type the following command:



The command used was "sudo make -f blink.c.make". You can see the two new files in the folder.

Now to put the code on the board you need to run the same command but also type "program-usbtiny" so the full command is "sudo make -f blink.c.make program-usbtiny"



The final result:



Now that I know the procedure, it is time for me to make the third version of my board and adapt the c-code that Neil has written for the two inputs so that it works with the atiny-44 controller. Using the c code may be a bit abitious for me so i may resort to arduino if it gets tough.

I milled the third version of my board and got to work on recycling the components from the second board using a heat gun. In no time at all the board was soldered. To my annoyance I had failed to correct the issue about the hall effect sensor not being connected to a pin on the mocro controller so I focused on getting the phototransistor to work as time was ticking.



It was now time to get some code on the board. I started with arduino and burned the bootloader with the necesary settings. Next was to write some arduino code. I adapted an if statement template so that the RBG would light up in dark conditions. However, the code wasn't working. I asked Emma, one of the instructors for some help and it turns out that for the arduino to work correctly, it needs a much more accurate clock than the default one, or one with less of a percentage error. A way around this would have been to add an external 20MHZ clock but it was a bit late for that. I didn't add the clock because I intended to use c code which wouldn't have a problem with this. Thankfully there was a solution to the temporary issue in the for of another arduino program written by Zaerc, our coding guru. He wrote a fantastic bit of code which indicade the line of code required to make the clock accurate. The code is as follows:

 

//HardwareSerial mySerial=Serial;  // RX==0, TX==1

//TinyDebugSerial mySerial=Serial; // RX!=4, TX==3

#include "SoftwareSerial.h"
SoftwareSerial mySerial( 7, 4); // RX, TX

//#include <SoftSerial.h>
//#include <TinyPinChange.h>
//SoftSerial mySerial( 0, 1); // RX, TX (like UsiSerial)
//SoftSerial mySerial( 5, 2); // RX, TX (tinyFly)
//SoftSerial mySerial( 1, 2); // RX, TX (FTDI breakout)
//SoftSerial mySerial( 4, 3); // RX, TX (TinyTuner)
//#include <EEPROM.h>
//#include <TinyOscCal.h>

//#include <UsiSerial.h>
//USISerial mySerial=UsiSerial; // RX==0, TX==1

//#define BAUD
//#define BAUD   300
//#define BAUD  9600
#define BAUD 19200

byte factory;
int offset;
char dir;


void setup()
{ 

  //Initialize serial and wait for port to open:
  mySerial.begin( BAUD);

//TinyOscCal.init( &mySerial, 0, 1);
//  TinyOscCal.init( &mySerial, 0, 0);

/** /
  mySerial.println("\nHello serial!");
  mySerial.print("OSCCAL=0x");
  mySerial.println(OSCCAL, HEX);
/**/


  factory=OSCCAL;
  offset=0;
  dir=1;

//  tinyCal();
}

void loop()
{
  tinyCal();
/*
  if (mySerial.available())
    mySerial.write(mySerial.read());
*/
}

void tinyCal()
{
/**/
  if( offset < -20 )
    dir=1;
  if( offset > 20 )
    dir=-1;
/**/
  offset+=dir;
  
  OSCCAL=byte(factory+offset);
  delay( 10);

  mySerial.print( "OSCCAL= 0x");
//  mySerial.print( factory+offset, HEX);
  mySerial.print( OSCCAL, HEX);
  mySerial.println( " hit <Enter>");
  delay( 1000);
  
}


Below is what shows in the serial montior when the code is run completely once. The lines starting with "OSCCAL" are the lines which have correctly calibrated the clock well enough for the serial communication to work and can then be used in other code. Zaerc said it is best to pick one from the centre of the pack. I picked "OSCCAL= 0x8B"

‚U…îV^+=GÒWT4AF‚[9äqZ$îVm*=GÒGt4AF‚[)UiPgÍiS÷6ƒGT6F' ‡[âQZ$ñV- yGÒGT6F' ‚[ UiP' iS,6ƒgT* Y](oÜP™ï^©ÖIx6∆gT*](ofP™ ÚIv≠J}≈„g‘*](obA4, ƒí]∏≠◊:g‘jô(ZAt, ≈í]x≠◊:ß÷*ëùH&˜ÀÇ¡µ\$]∏-ñ2ß÷jc}êHÓõ%ó·êN)GÕ∫O#Òß÷he}êLÓüJH¨ ≈Ÿ∏-ñ:ß÷|*E}êHÓõ2èÈêN)GÕ∫O#Òß÷\*ƒıÇa∏ü…H™- …Ÿº)ñ:)ˇß“‘( ’ÇA∏üd$] ⟺≤]*)ˇßÚ‘,ä⁄ÇAÒüÿPX*⟥)ñj)ˇØ©–Tä ÇAÒñ„êX*⟥)∂*)ˇß“T*ä‘Ç¡ÒüòêX*. ˘Èµ˘O#Òß÷T*∆’ÇAÒüòPZ. …*Ÿº-ñ*)ˇß“T*ƒÂÇAqü…H™- … Ÿº-ñ: ß÷|*EuêHÓüèÈêN)G’∫O#Òß÷leuêLÓõNh,ó ≈Ÿ∏-ñ* ß÷jb}êLÓõ%óÈêN©OÕ∫O#Òß÷*ëùH&˜ÀÇ¡µ\$]∏-ñ2gT*]h⁄At. ‰í]x≠◊2gV*›‚At, ƒ]8-ñ2gT* ›(ofP™ ra++·^°¸gT* Y_ oÜP™ó\©ÖMx6∆GT: Y_$ äP™ñ^©ÖMX2ƒGT2 Y_$ ˜éP™ÜA^â≈iX2ƒGt4AF‚[)UiPgÍiS÷6ƒGtAF‡K9äqZîV_*=GÒWtAV‡K ¿U»ñV_+=GÒOSCCAL= 0x80 hit
OSCCAL= 0x81 hit
OSCCAL= 0x82 hit
OSCCAL= 0x83 hit
OSCCAL= 0x84 hit
OSCCAL= 0x85 hit
OSCCAL= 0x86 hit
OSCCAL= 0x87 hit
OSCCAL= 0x88 hit
OSCCAL= 0x89 hit
OSCCAL= 0x8A hit
OSCCAL= 0x8B hit
OSCCAL= 0x8C hit
OSCCAL= 0x8D hit
OSCCAL= 0x8E hit
OSCCAL= 0x8F hit
OSCCAL= 0x90 hit
OSCCAL= 0x91 hit
OSCCAL= 0x92 hit
OSCCAL= 0x93 hit
OSCCAL= 0x94 hit
OSCCAL= 0x95 hit
OSCCAL= 0x96 hit
äO”√√¡ÃΩ†∞¯π7†ËÈÙ† OS√C¡Ã=†∞¯π∏†ËÈt†<≈ntÂÚ>çäè”ÉÉÅåΩ†∞¯ππ†®©¥†º≈ÓÙ•≤æçäOS√√¡L=†∞xπ∏ ËiÙ†º≈ÓÙer> 䜔C√¡Ã=†∞xπ7†hÈt†
çäOSCCAL= 0x96 hit
OSCCAL= 0x95 hit
OSCCAL= 0x94 hit
OSCCAL= 0x93 hit
OSCCAL= 0x92 hit
OSCCAL= 0x91 hit
OSCCAL= 0x90 hit
OSCCAL= 0x8F hit
OSCCAL= 0x8E hit
OSCCAL= 0x8D hit
OSCCAL= 0x8C hit
OSCCAL= 0x8B hit
OSCCAL= 0x8A hit
OSCCAL= 0x89 hit
OSCCAL= 0x88 hit
OSCCAL= 0x87 hit
OSCCAL= 0x86 hit
OSCCAL= 0x85 hit
OSCCAL= 0x84 hit
OSCCAL= 0x83 hit
OSCCAL= 0x82 hit
OSCCAL= 0x81 hit
OSCCAL= 0x80 hit
WTCVâ‡K ‚U…îV^*=GÒGT4AF ‚[9äqZ$îVm*=GÒGt4AF‚[)UiPgÍiS÷6ƒGT6F ‡[âQZ$ñV- yGÒGT6D' ‚[(UiP' iS,6ƒgT* Y_(oÜP™ï^©ÖIX2ƒ

I then put that in my code so that it would now work (plus a few other tweaks). The code is as follows:


SoftwareSerial mySerial(7, 4);
// These constants won't change:
const int analogPin = 5;    // pin that the sensor is attached to
const int ledPin = 3;       // pin that the LED is attached to
const int threshold = 400;   // an arbitrary threshold level that's in the range of the analog input

void setup() {
  OSCCAL= 0x8B;
  // initialize the LED pin as an output:
  pinMode(1, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(5, INPUT);
  pinMode(3, OUTPUT);  // initialize serial communications:
  mySerial.begin(9600);
  delay(1);
}

void loop() {
  // read the value of the potentiometer:
  int analogValue = analogRead(5);

  // if the analog value is high enough, turn on the LED:
  if (analogValue > threshold) {
    digitalWrite(3, LOW);
     digitalWrite(2, LOW);
      digitalWrite(1, LOW);
  }
  else {
    digitalWrite(3, HIGH);
     digitalWrite(2, HIGH);
      digitalWrite(1, HIGH);
  }

  // print the analog value:
  mySerial.println(analogValue);
  delay(1);        // delay in between reads for stability
}



You can see the code in action in this video and the serial communication:



You could say that I have completed the assignment for this week! Oh wait.. must not to forget the design files. However I really want to learn some more C code so there will be more documentation to come regarding that.