1.1 Arduino_Project 1_Code and Descriptions
1.1 Arduino_Project 1_Code and Descriptions
In this project, we will control the state of a LED light. For this, we will use an
Arduino Uno to switch the LED on and off with a delay of e.g., three (duration of
the LED's glow) or five (switched off state) seconds.
Required components:
4x jumper wires
1x LED
Wiring diagram:
We connect the LED to the Arduino using a resistor, the cables, and the
breadboard as shown on the next page.
For this, we first supply the breadboard with power. We connect a red cable to
the 5V pin of the Arduino board and the other end of the cable we plug into the
breadboard as shown in the picture. We also connect a black cable to the GND
pin of the Arduino board and the other end of the cable we plug into the
breadboard as shown in the picture.
Then we place the LED (shorter leg of the LED to the resistor) and the resistor as
shown and connect the LED with a black wire to the ground of the board. We
need the resistor to limit the current. Here we use Ohm's law and the formula R =
U / I. R stands for the resistance, U for the voltage and I for the current. Finally,
we need a yellow cable (can also be another color), which goes from the LED to
the board pin (2to be found at the digital pins above the Arduino logo).
Arduino | Step by step --- This document is protected by copyright. All rights reserved.
2
Arduino | Step by step --- This document is protected by copyright. All rights reserved.
// We first declare the variable "led_pin" and assign it to the pin to which we have
connected the LED (pin 2)
void setup() {
// Here we enter the setup code to be executed only once. We want the led_pin to
be defined as an output pin, i.e., to receive an output signal so that the LED lights
up, i.e. :
pinMode(led_pin,OUTPUT);
}
void loop() {
// The loop then executes the program code repeatedly, i.e. the LED repeatedly
switches off and on with the previously defined delay
// End of the program code. Tip: Check the correct number of { and }
Practice Task:
As an exercise, try switching the LED on and off at this point so that an SOS signal
is sent! (SOS signal: three times short, three times long, three times short). Long
signal = 2 seconds. Short signal = 1 second. Distance between short and long = 0.5
seconds. Distance of 5 seconds between several SOS signals (You will find the
solution on the next page).
3
Arduino | Step by step --- This document is protected by copyright. All rights reserved.
Solution:
We only have to change the part in void loop() in the above program code. For
the SOS signal, this could look like this. A short signal is defined in the following,
e.g., with 1 second, a long one with 2 seconds (LED on). Between the signals,
there should be 0.5 seconds for disconnection (LED off).
void loop()
{
4
Arduino | Step by step --- This document is protected by copyright. All rights reserved.
delay(5000); // to separate between several SOS signals e.g. wait 5000 milliseconds
(= 5 seconds)
// the loop function then executes the SOS signal repeatedly and permanently
Attention: When you try the code, don't forget the rest of the code structure as
in the first project (identical). (Hint: Just replace the part inside "void loop ( )"
from the first project with the code from here).
Excellent! We have successfully completed the first project! Let's move on to the
second project. This one will be a bit more difficult.