Basic Circuits – Lab 2 Arduino and Sensors Xmedia Spring 2011
Agenda Resistive Sensors Serial Communication Processing
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 int val = 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
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 – Resistive Sensor // 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 int val = 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
Arduino – Notes about code analogRead(pin); Returns 0 to 1023 analogWrite(pin, val); Max val = 255 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 }
Arduino – Serial Communication #define LED 9 int val = 0; void setup() { Serial.begin(9600); pinMode(LED, OUTPUT); } void loop() { val = analogRead(0); Serial.println(val); analogWrite(LED, val/4); delay(10); import processing.serial.* Serial sPort; int val; void setup() { println(Serial.list()); //find the port that is your arduino sPort = new Serial(this, Serial.list()[2], 9600); //set the position of the array so it corresponds //to your arduino } void draw() while(sPort.available() > 0) val = sPort.read(); println(val); background(val);
Processing – Notes about code Serial sPort; //creates the serial port object sPort = new Serial(this, Serial.list()[2], 9600); //instantiates the object and //opens the port sPort.available(); //returns the number of bytes in the buffer to be read sPort.read(); //returns the next byte in the buffer Each value added to the buffer by the Arduino is one byte (0 to 1023). The buffer may contain more than one byte. If you need to transmit data that requires more than one byte, you need to set up a protocol. http://www.processing.org/learning/library/serialcallresponse.html void serialEvent(Serial sPort) {} //event method called when data is //available http://www.processing.org/reference/libraries/serial/serialEvent_.html
Scaling Function
Lighting 3 LEDs in Parallel Each LED gets its own resistor Build this circuit Measure the voltage across each branch Measure the current out of the battery and before each LED
Current Split - Parallel Sum of the current through each branch equals the current from the power source Voltages are the same in each branch
Lighting 3 LEDs in Series One resistor for all the LEDs Build this circuit Measure the voltage across each LED Measure the current out of the battery and before each LED
Voltage Split - Series Voltage across each component is different Current through each component is the same
Voltage Divider Vout = Vin * R2 / (R1 + R2) If R1 is variable, as R1 increases Vout decreases
Calculating Resistance Series Rtotal = R1 + R2 + . . . + Rn Paralell 1/Rtotal = 1/R1 + 1/R2 + … + 1/Rn