Basic Circuits – Lab 2 Arduino and Sensors Xmedia Spring 2011
Agenda Parallel vs. Series Voltage Divider Arduino Uploading code Basics Connecting to a circuit PWM Sensors Reading analog data
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 Voltage drops are the same across 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 V out = V in * R 2 / (R 1 + R 2 ) If R 1 is variable, as R 1 increases V out decreases
Calculating Resistance Series R total = R 1 + R R n Paralell 1/R total = 1/R 1 + 1/R 2 + … + 1/R n
Arduino
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 // LED connected to // digital pin 13 void setup() { pinMode(LED, OUTPUT); // sets the digital // pin 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 analogWrite(pin, val); Connect LED between pin 9 and GND 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 int i = 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); 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 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 }