Lab 4 Audrey


1. a. Post a copy of your new code in your lab writeup.

/*

Analog Input

Demonstrates analog input by reading an analog sensor on analog pin 0 and

turning on and off a light emitting diode(LED) connected to digital pin 9.

The amount of time the LED will be on and off depends on

the value obtained by analogRead().

The circuit:

* Potentiometer attached to analog input 0

* center pin of the potentiometer to the analog pin

* one side pin (either one) to ground

* the other side pin to +5V

* LED anode (long leg) attached to digital output 9

* LED cathode (short leg) attached to ground

* Note: because most Arduinos have a built-in LED attached

to pin 9 on the board, the LED is optional.

Created by David Cuartielles

Modified 4 Sep 2010

By Tom Igoe

This example code is in the public domain.

http://arduino.cc/en/Tutorial/AnalogInput

*/

 

int sensorPin = A0; // select the input pin for the potentiometer

int ledPin = 9; // select the pin for the LED

int sensorValue = 0; // variable to store the value coming from the sensor

 

void setup() {

// declare the ledPin as an OUTPUT:

pinMode(ledPin, OUTPUT);

}

 

void loop() {

// read the value from the sensor:

sensorValue = analogRead(sensorPin);

// turn the ledPin on

analogWrite(ledPin, sensorValue);

}

a. Based on the readings from the serial monitor, what is the range of the analog values being read?

0-1023

b. How many bits of resolution does the analog to digital converter (ADC) on the Atmega32U4 have [hint: where might you look to find this sort of thing]? How many are you using with the range of values you're seeing?

According to the data sheet, the Atmega 32U4 has 2 to 16 bits of resolution. As our range is from 0 to 1023, we are probably using 10 bits.

a. What resistance do you see with a Multimeter when the sensor is flat? When it is bent?

14-15 kΩ straight 53-55kΩ bent

b. What kind of voltages should we expect for the Teensy analog pin based on the sensor resistance? 

Straight

Rtotal=15+22=37 kΩ I=VR=537k=0.135mA Vflex= 0.135 ×10-3×15×103 =2.035V ≈2V

Bent

Rtotal=55+22=77 kΩ I=VR=577k=6.49 ×10-5A Vflex=6.49 ×10-5×15×103 =0.035695 ≈0V 

c. How does the range of the LED's brightness change compared to the potentiometer?

 

The range of the LEDs brightness using the flex sensor is much lower as the range is only 2V whereas the potentiometer gives the full 5V range.

✔ Why do you think this is so?

 

d. Include a copy of your Lowly Multimeter code in your lab write-up.

 

 

/*

Analog Input

Demonstrates analog input by reading an analog sensor on analog pin 0 and

turning on and off a light emitting diode(LED) connected to digital pin 9.

The amount of time the LED will be on and off depends on

the value obtained by analogRead().

The circuit:

* Potentiometer attached to analog input 0

* center pin of the potentiometer to the analog pin

* one side pin (either one) to ground

* the other side pin to +5V

* LED anode (long leg) attached to digital output 9

* LED cathode (short leg) attached to ground

* Note: because most Arduinos have a built-in LED attached

to pin 9 on the board, the LED is optional.

Created by David Cuartielles

Modified 4 Sep 2010

By Tom Igoe

This example code is in the public domain.

http://arduino.cc/en/Tutorial/AnalogInput

*/

 

int sensorPin = A0; // select the input pin for the potentiometer

int ledPin = 9; // select the pin for the LED

int sensorValue = 0; // variable to store the value coming from the sensor

 

void setup() {

// declare the ledPin as an OUTPUT:

pinMode(ledPin, OUTPUT);

}

 

void loop() {

// read the value from the sensor:

sensorValue = analogRead(sensorPin);

int LEDValue = map(sensorValue,950,1023,0,5);

// turn the ledPin on

analogWrite(ledPin, LEDValue);

}

a. What resistance values do you see from your force sensor?

It has about 500Ω of resistance

✔ When force applied?

b. What kind of relationship does the resistance have as a function of force applied? (eg, linear?)

Logarithmically

✔ inverse or proportional?

c. Include a copy of your FSR thumb wrestling code in your lab write-up.

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int sensorPin = A0;

int sensor2Pin = A1;// select the input pin for the potentiometer

int ledPin = 9; // select the pin for the LED

int sensorValue = 0; // variable to store the value coming from the sensor

int sensor2Value = 0; // variable to store the value coming from the sensor

 

void setup() {

// declare the ledPin as an OUTPUT:

pinMode(ledPin, OUTPUT);

lcd.begin(16, 2);

}

 

void loop() {

lcd.setCursor(0, 0);

// read the value from the sensor:

sensorValue = analogRead(sensorPin);

sensor2Value = analogRead(sensor2Pin);

lcd.print("Player 1:");

lcd.print(sensorValue);

lcd.print(" ");

lcd.setCursor(0,1);

lcd.print("Player 2:");

lcd.print(sensor2Value);

lcd.print(" ");

}

 

a. Describe the voltage change over the sensing range of the sensor. A sketch of voltage vs. distance would work also. Does it match up with what you expect from the datasheet?

 

 

 

yes, according to the graph in the data sheet, the voltage should look like this. It shows that it has a peak at about 5 cm and voltage drops if any closer. Anything further than 5 cm exponentially decreases. However, in the data sheet, the voltage seems to level off near 0.5 V, whereas the voltage logged in my test drops to about 0.1 V.

a. Include your accelerometer read-out code in your write-up.

 

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

 

const int xpin = A3; // x-axis of the accelerometer

const int ypin = A2; // y-axis

const int zpin = A1; // z-axis (only on 3-axis models)

void setup()

{

// initialize the serial communications:

Serial.begin(9600);

lcd.begin(16, 2);

}

void loop()

{

lcd.setCursor(0, 0);

lcd.print(" X Y Z");

// print the sensor values:

int xValue = (analogRead(xpin));

Serial.print(analogRead(xpin));

// print a tab between values:

Serial.print("\t");

int yValue = (analogRead(ypin));

Serial.print(analogRead(ypin));

// print a tab between values:

Serial.print("\t");

int zValue = (analogRead(zpin));

Serial.print(analogRead(zpin));

Serial.println();

// delay before next reading:

lcd.setCursor(0, 1);

lcd.print(xValue);

lcd.print(" ");

lcd.print(yValue);

lcd.print(" ");

lcd.print(zValue);

 

delay(100);

}

 

 

a. Turn in a copy of your final state diagram.

This was my initial final state diagram

This the cleaned up version:

✔ Good job cleaning it up. Nice state diagram. Try to circle states to make it clearer.

 

a. How many byte-sized data samples can you store on the Atmega32U4?

255 bytes

Note quite. It can store 1k bytes. Check the datasheet. 

 

b. How would you get your analog data from the ADC to be byte-sized?

You would change the analog data to digital so ranges are required to be set on the analog.

?? What do you mean? The ADC is 10bit, so goes from 0-1023. A byte representation goes from 0-255. So simply divide the analog value by 4 to get it to be byte-sized.