Basic Circuits Lab 2 Arduino and Sensors Xmedia

Basic Circuits – Lab 2 Arduino and Sensors Xmedia Spring 2011

Agenda • Resistive Sensors • Serial Communication – Processing

Arduino – Reading Analog Sensor Data // Example 06 B: 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() { pin. Mode(LED, OUTPUT); // LED is as an OUTPUT // Note: Analogue pins are // automatically set as inputs } void loop() { val = analog. Read(0); // read the value from // the sensor Serial. println(val); analog. Write(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 ~11 k -> ~30 k (straight -> bent) Photo resistor (i. e. light sensor) Resistance increases when covered ~2 k -> ~60 k (lots of light -> little light) Force sensitive resistor (FSR, i. e. pressure sensor) Resistance decreases with pressure ~4 k -> ~0. 5 k (light touch -> lots of pressure) Different sensors have different specifications Read the datasheet or test using a multimeter

Arduino – Resistive Sensor // Example 06 B: 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() { pin. Mode(LED, OUTPUT); // LED is as an OUTPUT // Note: Analogue pins are // automatically set as inputs } void loop() { val = analog. Read(0); // read the value from // the sensor Serial. println(val); analog. Write(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 • analog. Read(pin); – Returns 0 to 1023 • analog. Write(pin, val); – Max val = 255 void loop() { val = analog. Read(0); // read the value from the sensor Serial. println(val); analog. Write(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); pin. Mode(LED, OUTPUT); } void loop() { val = analog. Read(0); Serial. println(val); analog. Write(LED, val/4); delay(10); } import processing. serial. * Serial s. Port; int val; void setup() { println(Serial. list()); //find the port that is your arduino s. Port = new Serial(this, Serial. list()[2], 9600); //set the position of the array so it corresponds //to your arduino } void draw() { while(s. Port. available() > 0) { val = s. Port. read(); println(val); background(val); } }

Processing – Notes about code • • Serial s. Port; //creates the serial port object s. Port = new Serial(this, Serial. list()[2], 9600); //instantiates the object and //opens the port s. Port. available(); //returns the number of bytes in the buffer to be read s. Port. 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 serial. Event(Serial s. Port) {} //event method called when data is //available – http: //www. processing. org/reference/libraries/serial. Event_. 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 * R 2 / (R 1 + R 2) • If R 1 is variable, as R 1 increases Vout decreases

Calculating Resistance • Series – Rtotal = R 1 + R 2 +. . . + Rn • Paralell – 1/Rtotal = 1/R 1 + 1/R 2 + … + 1/Rn
- Slides: 15