540 likes | 1.55k Views
Sensors, Arduino , Data. Xmedia Spring 2012. Agenda. Sensors Flex Sensor Force Sensing Resistor (FSR) Arduino Uploading code Basics Connecting to a circuit PWM Sensors Reading analog data using the serial monitor Methods for scaling the data. Flex Sensor. Variable resistor
E N D
Sensors, Arduino, Data Xmedia Spring 2012
Agenda • Sensors • Flex Sensor • Force Sensing Resistor (FSR) • Arduino • Uploading code • Basics • Connecting to a circuit • PWM • Sensors • Reading analog data using the serial monitor • Methods for scaling the data
Flex Sensor • Variable resistor • Changes resistance based on the angle of the bend. • Range: 10 kΩ to 110 kΩ • Positive and negative legs. Orientation does not matter.
Resistive Sensors Flex or bend sensor Resistance increases with bend ~11k -> ~30k (straight -> bent) Photo resistor (i.e. light sensor) Resistance increases when covered ~2k -> ~60k (lots of light -> little light) Force sensitive resistor (FSR, i.e. pressure sensor) Resistance decreases with pressure ~4k -> ~0.5k (light touch -> lots of pressure) Different sensors have different specifications Read the datasheet or test using a multimeter
Arduino – Environment • Board • Sometimes it is actually the board you have. • Other times it is not and you have to guess. • Serial Ports • Mac - /dev/cu/whatever • PC – usually COM 5
Arduino – Blinking an LED • Connect LED between pin 13 and GND • Connect Arduino to computer • Upload Code • Compile – play button • Select port – dev/cu. Or COM • Upload – right arrow button
Arduino – Blinking an LED // Example 01 : Blinking LED #define LED 13 //Sets the name LED to pin 13 void setup() { pinMode(LED, OUTPUT); // sets the digital pin LED as output } void loop() { digitalWrite(LED, HIGH); // turns the LED on delay(1000); // waits for a second digitalWrite(LED, LOW); // turns the LED off delay(1000); // waits for a second }
Arduino – Fading an LED using PWM • PWM – pulse width modulation • Approximates analog behavior by turning on and off quickly • Min 0; Max 255 • analogWrite(pin, val); • automatically sets the digital pin to use PWM • Connect LED between pin 9 and GND • Needs a resistor. Calculate the correct value. • Connect Arduino to computer • Write and upload code
Arduino – Fading an LED using PWM // Example 04: Fade an LED in and out like on // a sleeping Apple computer #define LED 9 // the pin for the LED inti = 0; // We’ll use this to count up and down void setup() { pinMode(LED, OUTPUT); // tell Arduino LED is an output } void loop(){ for (i = 0; i < 255; i++) { // loop from 0 to 254 (fade in) analogWrite(LED, i); // set the LED brightness delay(10); // Wait 10ms because analogWrite // is instantaneous and we would // not see any change } for (i = 255; i > 0; i--) { // loop from 255 to 1 (fade out) analogWrite(LED, i); // set the LED brightness delay(10); // Wait 10ms } }
Arduino – Reading Analog Sensor Data • analogRead(pin); • Min 0; Mac 1023 • Serial.println(val); - debugging for now • Build Circuit, connect Arduino, upload code • Serial monitor – to view debug values
Arduino – Reading Analog Sensor Data // Example 06B: Set the brightness of LED to // a brightness specified by the // value of the analogue input #define LED 9 // the pin for the LED intval = 0; // variable used to store the value // coming from the sensor void setup() { pinMode(LED, OUTPUT); // LED is as an OUTPUT // Note: Analogue pins are // automatically set as inputs } void loop() { val = analogRead(0); // read the value from // the sensor Serial.println(val); analogWrite(LED, val/4); // turn the LED on at // the brightness set // by the sensor delay(10); // stop the program for // some time }