0% found this document useful (0 votes)
4 views

1.1 Arduino_Project 1_Code and Descriptions

Uploaded by

vebogsm975
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

1.1 Arduino_Project 1_Code and Descriptions

Uploaded by

vebogsm975
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Project 1: A flashing LED and an SOS signal

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:

1x Arduino Uno Board

1x plug-in board (breadboard)

4x jumper wires

1x LED

1x 200 Ohm resistor

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.

Program code for the Arduino IDE:

// We first declare the variable "led_pin" and assign it to the pin to which we have
connected the LED (pin 2)

const int 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() {

// Here we enter the main code to be executed repeatedly (in a loop)

digitalWrite(led_pin,HIGH); // first supply led_pin with 5V (switches LED on).


delay(3000); // then wait 3000 milliseconds (= 3 seconds) .
digitalWrite(led_pin,LOW); // then supply led_pin with 0V (turns LED off).
delay(5000); // then wait 5000 milliseconds (= 5 seconds)
}

// 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()
{

// The three short signals follow first:

digitalWrite(led_pin,HIGH); // first supply the led_pin with 5V (switches LED on)


delay(1000); // wait for short signal 1000 milliseconds (= seconds1)
digitalWrite(led_pin,LOW); // then supply led_pin with 0V (switches LED off)
delay(500); // wait for e.g. milliseconds500 to separate (= seconds0.5)

digitalWrite(led_pin,HIGH); // supply led_pin with 5V again (switches LED on)


delay(1000); // wait for short signal 1000 milliseconds (= 1 seconds)
digitalWrite(led_pin,LOW); // then supply led_pin with 0V (switches LED off)
delay(500); // wait for separation e.g. 500 milliseconds (= 0.5 seconds)

digitalWrite(led_pin,HIGH); // supply led_pin with 5V again (switches LED on)


delay(1000); // wait for short signal 1000 milliseconds (= 1 seconds)
digitalWrite(led_pin,LOW); // then supply led_pin with 0V (switches LED off)
delay(500); // wait for separation e.g. 500 milliseconds (= 0.5 seconds)

// then the three long signals follow:

digitalWrite(led_pin,HIGH); // first supply led_pin with 5V (switches LED on)


delay(2000); // wait for long signal 2000 milliseconds (= 2 seconds)
digitalWrite(led_pin,LOW); // then supply led_pin with 0V (switches LED off)
delay(500); // wait e.g. 500 milliseconds for separation (= 0.5 seconds)

digitalWrite(led_pin,HIGH); // first supply led_pin with 5V (switches LED on)


delay(2000); // wait for long signal 2000 milliseconds (= 2 seconds)
digitalWrite(led_pin,LOW); // then supply led_pin with 0V (switches LED off)
delay(500); // wait e.g. 500 milliseconds for separation (= 0.5 seconds)

digitalWrite(led_pin,HIGH); // first supply led_pin with 5V (switches LED on)

4
Arduino | Step by step --- This document is protected by copyright. All rights reserved.

delay(2000); // wait for long signal 2000 milliseconds (= 2 seconds)


digitalWrite(led_pin,LOW); // then supply led_pin with 0V (switches LED off)
delay(500); // wait e.g. 500 milliseconds for separation (= 0.5 seconds)

digitalWrite(led_pin,HIGH); // first supply the led_pin with 5V (switches LED on)


delay(2000); // wait for long signal 2000 milliseconds (= 2 seconds)
digitalWrite(led_pin,LOW); // then supply the led_pin with 0V (switches LED off)
delay(500); // wait e.g. 500 milliseconds for separation (= 0.5 seconds)

// Finally, three short signals follow again:

digitalWrite(led_pin,HIGH); // first supply the led_pin with 5V (switches LED on)


delay(1000); // wait for short signal 1000 milliseconds (= 1 seconds)
digitalWrite(led_pin,LOW); // then supply the led_pin with 0V (switches LED off)
delay(500); // wait e.g. 500 milliseconds for separation (= 0.5 seconds)

digitalWrite(led_pin,HIGH); // supply led_pin with 5V again (switches LED on)


delay(1000); // wait for short signal 1000 milliseconds (= 1 seconds)
digitalWrite(led_pin,LOW); // then supply the led_pin with 0V (switches LED off)
delay(500); // wait for separation e.g. 500 milliseconds (= 0.5 seconds)

digitalWrite(led_pin,HIGH); // supply led_pin with 5V again (switches LED on)


delay(1000); // wait for short signal 1000 milliseconds (= 1 seconds)
digitalWrite(led_pin,LOW); // then supply the led_pin with 0V (switches LED off)

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.

You might also like