Week 11 - Input devices

Week 11 - Input devices

home

Group assignment

measure the analog levels and digital signals in an input device.

Here is a link to our lab's page which includes links to all our group assignments.

Individual assignment

measure something: add a sensor to a microcontroller board that you have designed and read it.

Assignment summary

what I achieved/learned this week:

Input devices, otherwise known as sensors, allow us to MEASURE behaviour. This can be sound, movement, heat, light, current, voltage etc. I wanted to get started with something easy, a quick win that would work, so I started with using a LM35 active temperature sensor (here's the datasheet) which came with an arduino kit I bought a while ago. I managed to get it to work with the arduino UNO from the kit. Here's the code:

/*
  Temperature Measurement
  Reads an analog input on A0(output from LM35DZ), change it to the 
  real room temperature and print it to serial monitor.
 */
float powervoltage=5;//define the power supply voltage.
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}
void loop() {
  float temperature;
  // read the input on analog pin 0:
  float sensorValue = analogRead(A0);
  // print out the value you read:
  temperature=(sensorValue/1023)*powervoltage*100;
  Serial.print("The room temperature degree is:");
  Serial.println(temperature,1);
  delay(2000);        // delay in between reads for stability
}

And here's the setup and the pin configuration.

LM35DZ temperature active sensor

LM35DZ temperature active sensor pins

and the overall setup.

And here is the output of the code.

serial output of temperature readings

After using the bought in arduino, I tried this with my own helloworld board. I thought that this would be quite straight forward, but for some reason I can't even get the serial monitor to work (even when simply trying to print values unrelated to the temperature measurement), the following error occurs: "'Serial' was not declared in this scope". I've searched some forums online and others have had the same problem with ATtiny44 chips. One suggestion was to call up the SoftwareSerial library using the following code:

#include LESSSoftwareSerial.hGREATERLESS/SoftwareSerial.hGREATER

But the same message appears. I think this is a sign that I need to create my own arduino board which has more capability than the current helloworld board. I connected it to my board, using pin 13 on my Attiny44 which corresponds with A0 on the arduino.

arduno attiny pins correlation diagram

In doing this I've also managed to fry my temperature sensor, when I plug it in now the values are not correct, and it gets very hot. I haven't measured the input/output, but my hunch is that it has fried the internal circuit and it now shorts itself out whenever I plug it in. I've now ordered a couple of new ones to play around with them, as it seemed to be quite a nice little package. So while I wait for the new sensors to arise, this has left me with some issues and questions to ponder:

In trying to get the serial monitor to work, I was referred to my guru Luiz's website with some code to try.

// Using an ATTiny44
#include 
SoftwareSerial mySerial(0,1);     // RX PA1 12, TX PA0 13 (flipped)

void setup() {
  mySerial.begin(19200);          // start serial feed, set the baud rate
}

void loop() {                     // print hello every 250ms
  mySerial.println("hello");
  delay(250);             
}

But this didn't work either. No error messages, it uploaded fine, but no data gets shown in the serial monitor at all. I also tried varying the baud rate in the serial monitor, using all the available values, but nothing changes. Then I tried to read data from the button, even as analog data using the following code.

// Using an ATTiny44
#include 
SoftwareSerial mySerial = SoftwareSerial (1,0);    // RX PA1 12, TX PA0 13

// Pin Configurations
const int BUTTON = A3;             // PA3
int sensorValue = 0;  // variable to store value coming from the sensor


void setup() {
  // declare the LED as an OUTPUT:
  pinMode(BUTTON, INPUT);
  mySerial.begin(9600);
  mySerial.println("The value is:");
}

void loop() {            // read the value from the sensor every 500 ms
  sensorValue = analogRead(BUTTON);
  mySerial.println(sensorValue);
}
            

But again, nothing showed on the serial monitor and I again tried all the baud rates as suggested by our guru.

Next up, to get something working, I used a sound sensor module to measure the loudness of the sound being detected by a microphone using an analogue input to the generic arduino board. This sensor also allows for a threshold noise level to be set for the background noise. Both of these inputs can be tuned independently using potentiometers on the sound sensor board itself.

sound sensor module

The Sound Sensor in the kit has 4 pins: GND,5V,A,D.

An interesting finding from this is the suggested code uses pin A0 for the analogue input, but instead of specifying A1 for the digital input, it uses in 15. After some searching it seems that these analogue input pins can also be used for digital input, where pin A0=pin 14, pin A1=pin 15 etc, up to pin A5=pin 19. Anyways, it worked! here's a video demonstrating that at 6am on a Sunday, Marvin Gaye is clearly below the noisy theshold, with only some nice analog signals, but when the Rolling Stones come on, the noisy theshold is reached! I also played around with the gain on the analogue

And here's the code I used to make the sound sensor read analogue and digital input.

/*
 Sound Sensor Playing
 Reads the analog output of Sound Sensor through A0, prints the result to the serial monitor 
 Read the digital output of Sound Sensor throug A1, prints the result to the serial monitor
 
 Connect the pins in the following way:
 GND----GND
 VCC----5V
 A------A0
 D------A1
 */

void setup() {
  Serial.begin(9600);
}

void loop() {
  int sensorValue = analogRead(A0); //Read the analog value of the sound sensor
  int digitalValue = digitalRead(A1);//Read the digital value of the sound sensor
  Serial.print("Analog Value=");
  Serial.println(sensorValue);//Print the analog value to serial monitor
  if(digitalValue){
  Serial.println("D is HIGH now");
  delay(5000);              // wait for a second
 }
}
            

here is the soundsensor.ino programme file to download

So now I'm moving on to creating a new board, a fab leo, in order to use the serial monitor, but also to give me good scope in the coming weeks for using multiple input/output devices, add an LCD screen, buttons, and ultimately a wifi module. Our fab guru Luiz suggested I start with a version of his Bueno Board, and he directed to his eagle files here.

Here's his board design that I will now mill out.

here is the PNG file of the OUTLINE to download

here is the PNG file of the TRACES to download

here is the AI file with TRACES and OUTLINE to download

ATmega 32u4 pins and how they relate to Arduino

I managed to find a special PCB drilling setup in C411, with 0.8, 1 and 1.2 mm drilling bits. They worked a treat! the holes can also be drilled in the milling machine.

and here's the board all done. After the traces were milled the outline for some reason was not in the right place, chopping off the top of the board. The traces were still there (just), so I was very luckly. Luiz mentioned that this could have to do with the positioning of my outline in Illustrator, but I will need to check this out. I don't want this to happen again.

It's also worth noting that early I was quite happy with my soldering and the way it all came together, UNTIL I realised I'd forgotten to solder one side of the chip onto the board, and also I'd forgotton to solder the pins from the USB connector onto the board. These careless omissions caused a bit of grief, in particular soldering the remaining side of the chip with the female connectors in the way was very problematic - many pins got soldered together and I needed help. Luiz showed me how the copper wick can be used to draw excess solder out, but that was still difficult and the result was not pretty.

the bongo board black and white

The first thing to do was to burnbootloader using my own programmer so I can then connect the bongoboard directly to the laptop with the need for the programmer anymore, here are the processes courtesy of Luiz's website again:

Then the board has been programmed!

Then of course the first test is to see if we can get the LEDs blinking...successs!

The idea is now to solder a 100K Ohm NTC thermistor (shown below) directly to the board and I will use this to do some tests for my final project. I will also aim to then make another board to be able to solder in a relay shield, and some capacitive touch button, then finally a wifi shield also, but that will have to be next week.

100K Ohm NTC thermistor

Here's the board with the thermistor mounted on it.

I then used a highly advanced experimental setup to calibrate my analogue output voltages against known (but crude!) temperatures.

Here is the calibration graph I used to get started. I find using the linear regression fit to generate the y=f(x) equation the quickest way to generate the calibration formula. Here's the graph:

and here the temperature readings, this time using my own board that I made and using my own basic calibration curves.

and here is the code used to generate this temperature data.

// the value of the 'other' resistor
#define SERIESRESISTOR 10000    
 
// What pin to connect the sensor to
#define THERMISTORPIN A5 
 
void setup(void) {
  Serial.begin(9600);
}
 
void loop(void) {
  float reading;
 
  reading = analogRead(THERMISTORPIN);
 
  Serial.print("Analog reading "); 
  Serial.println(reading);
 
  // convert the value to resistance
  reading = -0.225*reading+231.65;     // formula to convert analogue reading to temperature (Deg C)
  Serial.print("The temperature is "); 
  Serial.print(reading);
  Serial.println(" Degrees C"); 
  Serial.println(" "); 
  
 
  delay(1000);
}

here is the thermistor calibration programme.ino file to download

measuring voltage across 100k Ohm thermistor using voltage divider with 10k Ohm resistor in series with the thermistor. Here's the circuit for this.

notes for myself: Some useful references for this week are Adhitya's page, and Travis Russett's page become more familiar with pull up resistors etc...some microcontrollers have these built in taht you enable or disable in the software need to understand pins better and how translate between attiny44 and arduino - A, D, ADC, other need to understand counters, registers DATASHEET! debouncing look at CGs notes. thermistors vs thermocouples: A thermistor is a temperature-sensitive resistor, a thermocouple generates a voltage proportional to the temperature (in K) for thermistors, don't use voltage change across the thermistor, use a bridge and measure the difference between the two sides of the bridge (starts at 0). synchronous detection (removing out ambient light from your detection)

Back to top