arduino tutorials
DESCRIPTION
Arduino TutorialsTRANSCRIPT
Arduino for Starter
Tutorials for the Arduino Kits from
www.funduino.de
This guide is in process. If you have any suggestions, please contact us: [email protected]
Funduino UG (Haftungsbeschränkt), 12.01.2015
1
Content
Programming..............................................................................................................3
1. Sketch No.1: A flashing LED..................................................................................3
2. Sketch No.2: Two flashing LEDs............................................................................5
3. Sketch No.3: sound and light.................................................................................6
4. Sketch No.4: A pulsating LED................................................................................7
5. Sketch No.5: Switch a LED on by pressing a pushbutton.....................................8
6. Sketch No.6: measure light intensity......................................................................9
7. Sketch No.7: Use a potentiometer to choose the flashing-speed of a LED.........11
8. Sketch No.8: Movement detection.......................................................................12
9. Sketch No.9: Temperature measurement............................................................14
10. Sketch No.10: Measurement of distance...........................................................17
11. Sketch No.11: Usage of a infrared remote.........................................................21
12. Sketch No.12: control a servo............................................................................25
13. Sketch No.13: Show a text on a LCD display....................................................27
2
Programming
1. Sketch No.1: A flashing LED
Required equipment: Only the Arduino board and a USB-cable.
void setup()
{
pinMode(13, OUTPUT);
}
void loop()
{
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
}
Here the setup begins
Pin 13 is a output.
Here the main program begins
Voltage (5V) high on pin 13
1000ms (1 second) delay
Voltage low on pin 13 (0V)
1000ms (1 second) delay
Now the loop starts again.
3
Upload the sketch on the Board.
1.4 Extension of the Sketch
The LED has to flash faster by using a shorter delay
void setup()
{
pinMode(13, OUTPUT);
}
void loop()
{
digitalWrite(13, HIGH);
delay(200);
digitalWrite(13, LOW);
delay(200);
}
4
2. Sketch No.2: Two flashing LEDs
Required equipment: Arduino / two LEDs (blue) / two resistors 100 Ohm / Breadboard / cables
void setup()
{
pinMode(7, OUTPUT);
pinMode(8,OUTPUT);
}
void loop()
{
digitalWrite(7, HIGH);
delay(1000);
digitalWrite(7, LOW);
digitalWrite(8, HIGH);
delay(1000);
digitalWrite(8, LOW);
}
Pin 7 is a output.
Pin 8 is a output.
Here the main program begins
Voltage (5V) high on pin 7
1000ms (1 second) delay
Voltage low on pin 7 (0V)
Voltage (5V) high on pin 8
1000ms (1 second) delay
Voltage low on pin 8 (0V)
Now the loop starts again.
5
3. Sketch No.3: sound and light
Required equipment: Arduino / 1x LED / 1x resistor 200 Ohm / 1x Piezo-Speaker / Breadboard /
cables
int LED=4;
int beep=5;
void setup()
{
pinMode(LED, OUTPUT);
pinMode(beep,OUTPUT);
}
void loop()
{
digitalWrite(LED, HIGH);
digitalWrite(beep, HIGH);
delay(1000);
digitalWrite(LED, LOW);
digitalWrite(beep, LOW);
delay(1000);
The word „LED“ is now „4“
The word „beep“ is now „5“
Pin 4 (Pin „LED“) is a output.
Pin 5 (Pin „Pieps“) is a output.
Switch the LED on
Switch the piezo-speaker on
Wait one second
Switch the LED off
Switch the piezo-speaker off
Wait one second
6
}
4. Sketch No.4: A pulsating LED
int LED=9;
int brightness= 0;
int fadesteps= 5;
void setup()
{
pinMode(LED, OUTPUT);
}
void loop()
{
analogWrite(LED, brightness);
brightness = brightness + fadesteps;
delay(25);
if (brightness == 0 || brightness ==
255)
The function „analogWrite“ activates the PWM-function
7
{
fadesteps = - fadesteps ;
}
}
5. Sketch No.5: Switch a LED on by pressing a pushbutton
A LED has to be switched on for fife seconds after a pushbutton has been pressed.
Required equipment: Arduino / 1x LED (blue) / 1x resistor 100 Ohm / 1x resistor 1KOhm (1000 Ohm) /
Breadboard / cable / 1x pushbutton
int LEDblue=6;
int pushbutton=7;
int buttonstate=0;
void setup()
{
pinMode(LEDblue, OUTPUT);
8
pinMode(pushbutton, INPUT);
}
void loop()
{
buttonstate =digitalRead(pushbutton);
if (tasterstatus == HIGH)
{
digitalWrite(LEDblue, HIGH);
delay (5000);
digitalWrite(LEDblue, LOW);
}
else
{
digitalWrite(LEDblue, LOW);
}
}
Now the mode has to be “input”, because
the Arduino-board checks the incoming
voltage on that pin.
6. Sketch No.6: measure light intensity
If the light intensity is low (as possible in the night), the LED gets switched on
9
int intensity= A0;
int LED = 10;
int sensorvalue = 0;
void setup()
{
Serial.begin(9600);
pinMode (LED, OUTPUT);
}
void loop()
{
sensorvalue =analogRead(intensity);
Serial.print("sensorvalue = " );
Serial.println(sensorvalue);
if (sensorvalue > 512 )
{
digitalWrite(LED, HIGH);
}
else
{
digitalWrite(LED, LOW);
}
delay (50);
}
Activates the serial communication
„analogRead(intensity)“ reads the voltage on pin A0 (analog
0). The value gets saved as a number between 0 and 1023 (0
to 5 volt)
“Serial.print” sends informations to the „serial monitor“.
10
7. Sketch No.7: Use a potentiometer to choose the flashing-speed of a LED
int input= A0;
int LED = 13;
int sensorvalue = 0;
void setup()
{
pinMode (LED, OUTPUT);
}
void loop()
{
sensorvalue =analogRead(input);
digitalWrite (LED, HIGH);
delay (sensorvalue);
digitalWrite (LED, LOW);
delay (sensorvalue);
}
The voltage on the middle potentiometer-pin is in the range 0
volt to 5 volt. The Arduino-board will save it as a number
between 0 and 1023.
That value gets used by the delay. The number is now the
delay-time in milliseconds.
11
8. Sketch No.8: Movement detection
A piezo-speaker has to make a noise if a movement gets detected.
Left side: time of output in case of a
detected movement.
Right side: sensibility
1) Jumper outside: in case of a detected
movement, the output signal (5 volt)
holds for some time.
2) Jumper inside (picture): the output
signal is only active while a movement is
detected.
12
int piezo=5;
int movement=7;
int movestatus=0;
void setup()
{
pinMode(piezo, OUTPUT);
pinMode(movement, INPUT);
}
void loop()
{
movestatus =digitalRead(movement);
if (movestatus == HIGH)
{
digitalWrite(piezo, HIGH);
delay(5000);
digitalWrite(piezo, LOW);
Read the status of movement
If the voltage on the movement input-pin is high, the
piezo-speaker will make a noise.
13
}
else
{
digitalWrite(piezo, LOW);
}
}
9. Sketch No.9: Temperature measurement
We want to read the temperature with theTMP36 sensor. The temperature should be shown on the serial-monitor
Required equipment: Arduino / Breadboard / jumper-wire / temperaturesensor TMP36 / external power-supply
The sensor has three terminals. 5V, GND, and the pin for the temperature signal. On this pin, the sensor outputs a voltage between 0 and 2.0 volts. 0V = -50 ° C and 2.0V = 150 ° C.The voltage on this pin must be read by the microcontroller board and then it hast to be converted into a temperature value.
- CAUTION: If the sensor is connected incorrectly it gets destroyed.
- Use a external power supply for more sensor accuracy (as possible 9V battery).
int TMP36 = A0;
int temperature = 0;
int temp[10];
int time= 20;
void setup() {
The middle pin (signal) is connected to
analog pin A0.
Value for the temperature.
temp[10] creates ten values with the
names temp[1], temp[2], temp[3] and so
on...
The value „time“ is for the delay between
two measurements.
14
Serial.begin(9600);
}
void loop() {
temp[1] = map(analogRead(TMP36), 0, 410, -50, 150);
delay(time);
temp[2] = map(analogRead(TMP36), 0, 410, -50, 150);
delay(time);
temp[3] = map(analogRead(TMP36), 0, 410, -50, 150);
delay(time);
temp[4] = map(analogRead(TMP36), 0, 410, -50, 150);
delay(time);
temp[5] = map(analogRead(TMP36), 0, 410, -50, 150);
delay(time);
temp[6] = map(analogRead(TMP36), 0, 410, -50, 150);
delay(time);
temp[7] = map(analogRead(TMP36), 0, 410, -50, 150);
delay(time);
temp[8] = map(analogRead(TMP36), 0, 410, -50, 150);
delay(time);
temp[9] = map(analogRead(TMP36), 0, 410, -50, 150);
delay(time);
temp[10] = map(analogRead(TMP36), 0, 410, -50, 150);
temperature=(temp[1]+temp[2]+temp[3]+temp[4]+te
mp[5]
+temp[6]+temp[7]+temp[8]+temp[9]+temp[10])/10; //
everything in one line!!!!
Serial.print(temperatur);
Serial.println(" degree");
}
Starts the serial communication. It will
send the informations from the Arduino-
Board to the computer to show it there in
the serial monitor.
You can start the serial monitor in the
arduino-software with a click on „settings“
and „serial monitor“.
From here, the temperature gets measured
ten times. In the same line, the measured
voltage gets transformed in a number
between -50 and 150. The function is called
„map“.
The ten temperatures get addet and
divided with ten, to get a average
temperature.
The average temperature from the ten
measurements get send to the serial-
monitor.
15
9.1 Extension of the sketch:If the temperature reaches 30°C , a noise from the piezo-speaker appears.
int TMP36 = A0;
int temperature = 0;
int temp[10];
int time= 20;
int piezo=5;
void setup() {
Serial.begin(9600);
pinMode (piezo, OUTPUT);
}
void loop() {
temp[1] = map(analogRead(TMP36), 0, 410, -50, 150);
delay(time);
temp[2] = map(analogRead(TMP36), 0, 410, -50, 150);
….
temp[9] = map(analogRead(TMP36), 0, 410, -50, 150);
delay(time);
temp[10] = map(analogRead(TMP36), 0, 410, -50, 150);
temperature=(temp[1]+temp[2]+temp[3]+temp[4]+temp[
5]
+temp[6]+temp[7]+temp[8]+temp[9]+temp[10])/10; // all
in one line
Serial.print(temperatur);
Serial.println(" Grad Celsius");
if (temperatur>=30)
{
digitalWrite(piezo,HIGH);
}
Piezo-speaker on pin5.
Pin5 is a output.
If the temperature is above 30°C
the piezo gives a sound
or...
...it is quiet.
16
else
{
digitalWrite(piezo,LOW);
}
}
10. Sketch No.10: Measurement of distance
We want to measure the distance with the HC-SR04 ultrasonic sensor.
How does the ultrasonic sensor function?
The sensor has four pins.
a) 5V (+) b) GND (-) c) d echo) trigger
The connections 5V and GND are for the power supply. The Pin "trigger" gets a short signal (5V), and
creates a sound wave. As soon as the sound wave hits a wall or other objects, it will be reflected and
comes back to the ultrasonic sensor. When the sensor detects this returned sound wave, the sensor
sends a signal to the Arduino microcontroller by the "echo" pin. The Arduino-board measures the
time between the transmission and the return of the sound wave, and converts this time into a
distance.
Required equipment: microcontroller board / cable / Breadboard / Hc-SR04 ultrasonic sensor
17
int trigger=7;
int echo=6;
long time=0;
long dist=0;
void setup()
{
Serial.begin (9600);
„trigger“ on pin7.
„echo“ on pin 6.
The value „time“ will save the time between transmition an
returning of the soundwave.
The value „dist“ will save the calculated distance. It will start
with „0“. Instead of „int“ we use „long“ for this value, to save
a bigger number
Starts the serial communication. It will send the informations
from the Arduino-Board to the computer to show it there in
the serial monitor.
18
pinMode(trigger, OUTPUT);
pinMode(echo, INPUT);
}
void loop()
{
digitalWrite(trigger, LOW);
delay(5);
digitalWrite(trigger, HIGH);
delay(10);
digitalWrite(trigger, LOW);
time = pulseIn(echo, HIGH);
dist = (time/2) / 29.1;
if ( dist >= 500 || dist <= 0)
{
Serial.println("No
measurement");
}
else
{
Serial.print(dist);
Serial.println(" cm");
}
delay(1000);
}
"trigger" (Pin7) is a output.
"echo" (Pin6) is a input.
low voltage on the trigger pin to produce a clear signal.
...for 5 milliseconds.
Creating the soundwave.
...for 10 milliseconds.
Stop creating the soundwave.
With the command pulseIn " (with a big „i“ next to the last
„n“) the Arduino-board counts the time between sending and
receiving the soundwave.
This calculation transforms the measured time into the
distance in centimeters. (The sound needs 29,1 seconds for
one centimeter. The time is devided by two, because we only
want to receive only one distance and not the two ways, the
sound has to take)
If the distance is over 500cm OR under 0cm, the
measurement is not accurate. So the serial-monitor shows
„No measurement“
otherwise...
the calculated distance gets send to the serial-monitor.
This command causes a short break between the
measurements.
19
10.1 Extension of the sketch
If the distance is less than 80cm, a sound from the piezo-speaker should appear.
int trigger=12;
int echo=13;
long dauer=0;
long entfernung=0;
int piezo=5;
void setup()
{
Serial.begin (9600);
pinMode(trigger, OUTPUT);
pinMode(echo, INPUT);
pinMode(piezo, OUTPUT);
}
void loop()
{
digitalWrite(trigger, LOW);
delay(5);
digitalWrite(trigger, HIGH);
delay(10);
digitalWrite(trigger, LOW);
dauer = pulseIn(echo, HIGH);
entfernung = (dauer/2) / 29.1;
if (entfernung >= 500 ||
entfernung <= 0)
{
Serial.println("Kein Messwert");
}
else
{
Piezo-speaker on pin5
The pin for the speaker is a output
20
Serial.print(entfernung);
Serial.println(" cm");
}
if (entfernung <= 80)
{
digitalWrite(piezo,HIGH);
}
else
{
digitalWrite(piezo,LOW);
}
delay(1000);
}
If the distance is less than 80cm...
...the speakter makes some noise.
Otherwise...
it is quiet.
11. Sketch No.11: Usage of a infrared remote
With an infrared receiver, the Arduinoboard can receive the commands
of an infrared remote control. The data are sent with infrared light
from remote control to the receiver. Since our eyes can not perceive
this light, we can not see this light.
With a little trick you can see the light.
Take your mobile-phone and look with the camera on the infrared
diode of the remote while pressing a button on the remote.
You will see the flashing infrared diode on the display of the mobile-
phone.
Required equipment: Arduino / breadboard / cable / infrared sensor /
infrared remote control
21
22
The sketch is a variation of the sketch „IRrecvDemo“, an can be downloaded on the following link.
https://github.com/shirriff/Arduino-IRremote
You can download the zip-package and copy the files into your „libraries“ directory in the arduino-
software. Rename the downloaded directory to "Irremote".
Now you can open the sketch in the sample-files in the arduino-software:
File -> Examples -> IRremote -> IRrecvDemo
Now we edit the Sketch to this Sketch:
/*
* IRremote: IRrecvDemo - demonstrates
receiving IR codes with IRrecv
* An IR detector/demodulator must be
connected to the input RECV_PIN.
* Version 0.1 July, 2009
* Copyright 2009 Ken Shirriff
* http://arcfn.com
*/
#include <IRremote.h>
int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600);
pinMode (13, OUTPUT);
irrecv.enableIRIn();
}
void loop()
{
if (irrecv.decode(&results)) {
Serial.println(results.value, DEC);
irrecv.resume();
The signal-pin from the IR-Receiver is
connected to pin 11
23
}
}
Pressing the "1" key on the infrared remote control causes (in my case) the serial-monitor writes the
number "16724175". This is the decrypted number code behind this button.
When you hold the button permanently pressed, the number "4294967295" appears. This is the code
that indicates that a key is pressed continuously. This number does not depend on which key is
pressed.
There can also appear other numbers if a key is pressed only very short or pulsating. In the case the
sensor may not read unique value.
Extension of the sketch:
Switch on a LED by pressing button1 and switch it off with button2.
#include <IRremote.h>
int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600);
pinMode (13, OUTPUT);
digitalWrite(13, LOW);
irrecv.enableIRIn();
}
void loop() {
if (irrecv.decode(&results)) {
Serial.println(results.value, DEC);
if (results.value == 16724175)
{digitalWrite (13, HIGH);}
if (results.value == 16718055)
{digitalWrite (13, LOW);}
irrecv.resume(); // Receive the next value
On pin13 is a LED (output)
It starts with a switched off LED.
If the IR-receiver receives the number 16724175
(button1), the LED gets switched on.
If the IR-receiver receives the number 16718055
(button2), the LED gets switched off.
24
}
}
12. Sketch No.12: control a servo
A servo has to turn to three different positions. Between the movents is a short break.
Required equipment: A microcontroller board, a servo, three jumper wire
#include <Servo.h>
Servo servoblue;
void setup() { servoblue.attach(8);}
Include the servo library
the servo gets the name „servoblue“
The signal-line of the servo is on pin8
25
void loop() { servoblue.write(0);delay(3000);servoblue.write(90);delay(3000);servoblue.write(180);delay(3000);servoblue.write(20);delay(3000);}
Position1 with a angle of 0°break for 3 secondsPosition2 with a angle of 90°break for 3 secondsPosition3 with a angle of 180°break for 3 secondsPosition4 with a angle of 0°break for 3 seconds
13. Sketch No.13: Show a text on a LCD display
Required equipment: microcontroller board, potentiometer, some jumper wire , breadboard
Note: The potentiometer is needed to adjust the contrast.
A good cabling is very important, solder the cable to the LCD.
26
#include <LiquidCrystal.h> LiquidCrystal lcd(12, 11, 6, 5, 4, 3);
void setup() {lcd.begin(16, 2);} void loop() {lcd.setCursor(0, 0);
lcd.print("www.funduino.de");lcd.setCursor(0, 1);
Load the LCD-library
This LCD has 16 signs in two rows.
Startposition of the cursor on the LCD (0,0 = first character in the first row) . Write the text „www.funduino.de“. Startposition of the cursor on the LCD (0,0 = first character in the second row) .
27
lcd.print("good luck!!!");}
Write the text „good luck!!!“.
14. Sketch No.14: Use a relais shield
A relays is a switch, that can be activated with a low
current from the Arduino-board. So you can switch on
and off electrical things, that need much more power
than a Arduino-board can provide.
The relays need a permanent power supply with 5V+
and – (In the picture VCC and GND). On the „IN“-pin, the
switch can be activated by the Arduinoboard.
Dependent of the manufacturer, there has to be a LOW or HIGH signal from the arduino output-pin.
On the three terminals on the right side you can connect the cables from the electrical thing, you
want to switch on and off.
The relays connects the terminals „middle“ and „right“ while the relays is switched off and when it is
activated, it connects the terminals „middle“ and „leftt“.
For testing purpose, you can use the „blink“-sketch. Instead of the LED, you connect the output-pin
from the Arduino-board with the „IN“pin from the relay. With that sketch, the relays will switch on
and off in a 1 second rhythm.
void setup(){pinMode(13, OUTPUT);}void loop(){digitalWrite(13, HIGH);delay(1000);digitalWrite(13, LOW);delay(1000);}
28