ARDUINO FOR BEGINNERS Smitha Pisupati and Sushma Rao Apr 26 2015
What is Arduino ¨ An open source electronics platform ¨ Easy to use HW and SW ¨ Make interactive projects ¨ No electronics background required
The HW – Arduino UNO
The Hardware
The Software Download: http://arduino.cc/en/Main/Software
Blink an LED
Blink an LED ¨ Connect the Arduino to the PC/Mac ¨ Open the Blink example File -> Examples -> 01.Basics -> Blink ¨ Upload!
Let’s look at the code
Hey Arduino, Run this part when you power up! Hey Arduino, Run this part over and over, forever!
Set Pin 13 to output Set Pin 13 to HIGH Wait 1 second Set Pin 13 to LOW Wait 1 second
Connect an External LED Digital Output
Breadboard Connections in the breadboard Breadboard Internals http://www.crifan.com/arduino_and_bread_board/
External LED
What makes the LED glow?
Electricity!
Electricity!
Resistor ¨ Limits amount of charge flowing through the circuit ¨ Has a “resistance” ¨ Higher the resistance, lower the amount of charge flow
Schematic ¨ Circuit diagram ¨ Uses symbols for electronic components
Control the Blink Rate Analog Sensor
Meet the Potentiometer ¨ A.K.A The Pot ¨ Variable resistor
How does it work? ¨ Voltage divider ¨ Voltage at A0 is a fraction of 5V DC
Adding a potentiometer
Changes to Blink code ¨ In the loop function, add the following line: int delayVal = analogRead(A0); ¨ Change delay(1000) to the following: delay(delayVal); // delayVal will be a number between 0 and 1023.
Adding serial debug
Add Serial Debug ¨ Add the following line to the setup() function Serial. Serial.begin(9600); ¨ Add the following line to the loop() function Serial.println(delayVal);
Serial Monitor More details at http://www.arduino.cc/en/Reference/Serial ¨
Ultrasonic Distance Sensor Digital Sensor
Ultrasonic distance sensor ¨ 4 pins ¤ VCC ¤ GND ¤ Trigger ¤ Echo
Reading Values from the Sensor Add the following at the top of the file ¨ unsigned long distanceInCm = 0; Add the following lines of code in the setup() function ¨ pinMode (trigger, OUTPUT); pinMode(echo, INPUT); Add the following in the loop() function ¨ // Hold trigger pin high for 10 microseconds digitalWrite(trigger, HIGH); delayMicroseconds(10); digitalWrite(trigger, LOW); // Get the width of the pulse with pulseIn count = pulseIn(echo, HIGH); // get distance in cm. Divide by 148 for inches distanceInCm = count / 58;
Glowing the LED ¨ Add the following at the top of the file int ledPin = 13; ¨ Add the following lines of code in the setup() function pinMode(ledPin, OUTPUT); ¨ Add the following in the loop() function if(distanceInCm < 10) { digitalWrite(ledPin, HIGH); } else { digitalWrite(ledPin, LOW); }
Finally… ¨ Add the following at the end of the loop count = 0; // add a delay for stable behaviour delay(200);
Building the Theremin
Let’s Code It
Repeat the LED code 6 more times ¨ Add the following at the top of the file int ledPin1 = 13; int ledPin2 = 12; int ledPin3 = 11; …
Repeat the LED code 6 more times ¨ Add the following lines of code in the setup() function pinMode(ledPin1, OUTPUT); pinMode(ledPin2, OUTPUT); pinMode(ledPin3, OUTPUT); …
Repeat the LED code 6 more times ¨ Add the following in the loop() function if(distanceInCm < 10) { digitalWrite(ledPin1, HIGH); digitalWrite(ledPin2, HIGH); digitalWrite(ledPin3, HIGH); … } else { digitalWrite(ledPin1, LOW); digitalWrite(ledPin2, LOW); digitalWrite(ledPin3, LOW); … }
Test It! ¨ Upload and check that this works…
Adding an else-if if(distanceInCm < 10) { digitalWrite(ledPin1, HIGH); … } else if (distanceInCm < 12) { digitalWrite(ledPin1, HIGH); digitalWrite(ledPin2, LOW); digitalWrite(ledPin3, LOW); … } else { digitalWrite(ledPin1, LOW); … }
Notes to play ¨ link to source ¨ Copy the top part of the file that looks like this: #define NOTE_B0 31 #define NOTE_C1 33 #define NOTE_CS1 35 #define NOTE_D1 37 #define NOTE_DS1 39 #define NOTE_E1 41 #define NOTE_F1 44 #define NOTE_FS1 46 #define NOTE_G1 49
Adding the tone ¨ Connect the Piezo buzzer at pin 5 ¨ Add this line to the top of the file int piezo = 5; ¨ Add this to the setup() function pinMode(piezo, OUTPUT); ¨ Add this to the loop() function // To play the tone tone(piezo,<NOTE>); // to stop the tone noTone(piezo);
Make Music!
References ¨ http://www.arduino.cc/en/Reference/Tone ¨ http://www.arduino.cc/en/Tutorial/HomePage
Recommend
More recommend