Arduino Tutorial (page 4)

  

 

Page 1 Page 2 Page 3 Page 4 (Examples) Page 5 (Examples)
⚛ Introduction to Arduino ⚛ Ultrasonic Sensors ⚛ Input/Output Port Management Functions (Pins) ⚛ The Flashing LED ⚛ Led Chase Effect
⚛ Supply of necessary components ⚛ Arduino Shields ⚛ Input - Output Functions ⚛ Fade in-Fade out ⚛ Led Chase Effect with potentiometer
⚛ LEDs ⚛ Input/Output Ports (Pins) ⚛ Digital Outputs ⚛ Fade in-Fade out, Blink at peaks ⚛ Effects by detecting light (Led effect + light sensor)
⚛ Buttons ⚛ Power Supply ⚛ Digital Inputs ⚛ Gradual increase and decrease of brightness with a potentiometer ⚛ Using sound (Buzzer)
⚛ Potentiometers ⚛ Arduino (IDE) Installation ⚛ Analog Outputs (PWM pins) ⚛ Using the serial screen ⚛ Sound & Light Sensor
⚛ Piezo Buzzers ⚛ Programming Arduino - Basic Functions ⚛ Analog Inputs ⚛ Playing with colors (RGB Led) ⚛ Moving shafts (Servos)
⚛ Photoresistors (LDR) ⚛ Basic structures and programming functions ⚛ Time Function - delay () ⚛ Making an RGB Led ⚛ Moving Servos with a potentiometer
⚛ Servo Motors ⚛ Type of variables ⚛ Time Function - delayMicroseconds () ⚛ Traffic lights ⚛ Motor Control (DC Motor)
⚛ DC Motors ⚛ Programming Comments ⚛ Time Logging Function - millis () ⚛ Pedestrian traffic lights ⚛ Controlling 2 or more motors
    ⚛ Value Mapping Function - map () ⚛ Pedestrian traffic lights and stop button ⚛ Using ultrasound to measure a distance
    ⚛ The Serial Communication Port (Serial)    
    ⚛ Selection Structure    
    ⚛ Repeat Structure (for)    
    ⚛ Support Libraries    

 

Examples

Here are examples that implement key layouts in this environment to get familiar with the platform and the materials, so you can simply go ahead with more complex constructions.

 

 

The Flashing LED

We will try to make one LED blink every second, that is to receive a current for one second, not to receive a current for one second and repeat it indefinitely.

For the circuit we will need a LED and a resistor so that the current of 5V that passes will correspond to the operating voltage of the Led we use and we will not have a problem (eg destruction of the Led by hypertension). You can see the circuit in the following picture.

Piezo-Buzzers

The output of the current to the particular shape is Pin 10, while being careful the resistor is connected between the output and the Led so that it does not pass a large amount of current from it. Led is then connected. Usually LEDs have a longer leg that comes in from the time +, that is Pin 10 in our case. Finally, the circuit closes with the other led of the led in the GND slot symbolizing the time - for our circuit.

All that remains is to program our circuit according to what we have already mentioned. For convenience, you are given instructions in the form of steps:

1. State a variable representing your Pin and give it a value of 10.
2. You should set Pin10 as Exit.
3. We need to give 5V voltage to Pin10.
4. We will have to wait for 1 second during which the LED will light up.
5. We must stop the power supply (voltage 0V) at Pin10.
6. We will have to wait for 1 second during which obviously the LED is off.
Steps 1,2 should be done only once. Steps 3-6 should be repeated continuously.

The code:

int ledPin = 10;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(1000);
}

 

 

Fade in-Fade out

In this example we will use a led which will not light up with the maximum current as in the previous exercises but its intensity will be increased. It will start flashing slowly and gradually increase the volume until it reaches the maximum. Then, on the contrary, it will start to gradually decrease the volume until it reaches the lowest level.

For this task we will need a led and a resistance as described in the previous examples and shown in picture.

Piezo-Buzzers

For this task, we will need to connect our led to some of the Arduino PWM ports, which also work as analogue outputs. The possible values ​​they can give are from 0 to 255, simulating the possible voltages from 0 to 5V. So, you will need to program your port as an output and then use the analogWrite (Pin, Brightness) function to give Pin slowly the desired values, from 0 to 255. Be careful not to exceed these limits!

As you already know, you should:

1. Set a variable for your port and initialize the port as output.
2. Set a brightness variable with a default value of 0.
3. In each iteration incrementally (eg every 5) you increase the brightness, up to 255.
4. When it reaches 255, you must reset the brightness and restart from the beginning.

The code:

int ledPin = 10;
int brightness = 0;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
if (brightness == 255) {
brightness = 0;
};
analogWrite(ledPin, brightness);
brightness = brightness + 5;
delay(100);
}

 

 

Fade in-Fade out, Blink at peaks

In this example we will use our previous circuit, but we will change the way it flashes. The volume will gradually rise, and when it reaches the maximum we will cause the LED to blink twice and then the intensity gradually drops. When it goes out, it will blink twice again and the same procedure will start.

For this task we will need a led and a resistance as described in the previous examples and shown in next Figure.

Piezo-Buzzers

For this work we will use the same circuit of the previous worksheets, and we will only need to change the programming a bit, so that it flashes when it reaches the limits. Specifically:

1. Make the appropriate statements and initializations (variables, values, ports).
2. Start at brightness 0 and progressively (eg every 5) to 255.
3. When you reach the maximum brightness (255), the LED will blink twice.
4. Start gradually lower the brightness slowly (eg by -5) until it goes out, ie it reaches 0.
5. When you reach the minimum brightness (0), you will blink twice and restart the process again.

The code:

int ledPin = 10;
int brightness = 255;
int interval = 5; // The time interval that the brightness will go up or down

void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
if (brightness == 255 || brightness == 0) {
// blinks two times
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
delay(100);
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
delay(100);
/* Change the sign in space, so if it go up until now (positive) to start descending (negative) and vice versa */
interval = (-1)*interval;
};
analogWrite(ledPin, brightness);
brightness = brightness + interval;
delay(100);
}

 

 

Gradual increase and decrease of brightness with a potentiometer

In this example we will use a potentiometer to adjust the intensity with which our LED will light up. To do this we need to connect a led and a resistor to a digital pin, a potentiometer to an analog pin, and depending on the value that gives us (we read from) the potentiometer we will give corresponding intensity to the led.

For this task we will need a led, and a resistor connected to a digital pin (PWM). We also need to connect a potentiometer to an analog input (there are six as we have seen in Arduino Uno) from which we will read the potentiometer's values. For the potentiometer, the connection is simple, as shown in next figure. We need a source (5V), ground (GND) and the middle foot connected to an analog input. The values ​​that will come from there will range from 0 to 1023, so we have to make the corresponding conversion to give them as Pin's intensity (eg give the entry through 4). The potentiometer is also placed directly on the breadboard if it has the appropriate edges.

Piezo-Buzzers

Be aware of this exercise that you need to initialize the ports properly, ie with the OUTPUT and the INPUT potentiometer. Use a variable to enter the potentiometer value, and as already mentioned, because this value will range from 0 to 1023, to adjust it as an output to the led, divide it by 4. The commands you will need are AnalogWrite (PIN, Value) and AnalogRead (Pin). Note that the names of the analog Pin are A0, A1, ..., A5, while the digital ones are 0, 1, 2, .. 13.

1. Make the necessary statements and initializations (entry-exit).
2. In each iteration you will read the value from the potentiometer and give it as input (with the appropriate adjustment) to your led.
3. Use a slight delay to prevent the difference in brightness in the LED (eg 50 msec).

The code:

int ledPin = 10;
int potPin = A4;
int potValue = 0; // Variable for the value readed from the potentiometer
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(potPin, INPUT);
}
void loop() {
potValue = analogRead(potPin);
analogWrite(ledPin, potValue/4);
delay(50);
}

 

 

Using the serial screen

In this example we will use the serial screen, sending messages and variable values ​​from a program that we have loaded into our unit.

For the serial display and communication of the unit with our computer, we do not need any special connections other than the USB cable connection.

In this example you will write a simple program that will activate the serial port and send messages to the screen for display in order to familiarize you with the serial screen.

1. Activate the serial port in the setup () function at a data rate of 9600.
2. In the loop () function, ie in each iteration, send a message in the form "H epikoinwnia ksekinhse:" without a line break.
3. With a repeat process, enter values ​​in a variable from 1 to 20 and these values ​​send them to the serial screen to be printed on a line each.
4. Make a more complex message using text and variable value, using printing with and without line break when you need, eg. "Twra vlepw thn epanalhpsh: X", where X will display the corresponding value of the variable in each iteration.
5. Plug in your drive and, after selecting the necessary (drive type and port), load your program.
6. Next, turn on the serial screen and watch the messages that come out on it.
7. Please note that unfortunately Greek does not appear correctly.

Prompt: The serial screen can be used to see what happens during the execution of our code (debugging). For example, you can refer to the circuit and the program of the previous worksheet after connecting the serial screen:

1. When you read the value from the potentiometer you print it on the serial screen without a line break.
2. When adjusting the potentiometer value, enter it as the input to print it on the serial screen (note the gap to avoid sticking to the previous print) by changing the line.
3. So you will see the two values ​​on each line!

The code:

int i;
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.print("The Communication has started");
for(i=1;i<=20;i++) {
Serial.println(i);
delay(100);
}
for(i=1;i<=20;i++) {
Serial.print("Now I see the repeat");
Serial.println(i);
delay(200);
 }
}

 

 

Playing with colors (RGB Led)

In this example we will use an RGB led that we will program to get a range of colors.

For this task we will need an RGB led and three resistances, one for each color. Figure 26 shows the connection. Note the values ​​of the resistances, the value for the red is slightly different from the ones for green and blue. Also, note which pin will connect with which color. The longest (2nd) foot of the led goes to the ground (GND), the outer one (1) in the red, the inner side (3rd) in the green and the last outside (4th) in the blue. Also, for all colors we will need PWM ports rather than simple digital ones.

Piezo-Buzzers

For this task we will need to connect our led to some of the Arduino PWM ports, which work as we have seen as analog outputs. Specifically:

1. Make any initializations needed (variables, values, ports).
2. With a repetitive process you will gradually add values ​​to each led. With the help of your teacher try to write a repeat command that will vary the values ​​you use in the LEDs in various ways, e.g.

For (i = 0; i <= 255; i = i + 5) {
AnalogWrite (redPin, i);
AnalogWrite (greenPin, i);
AnalogWrite (bluePin, i);
Delay (200);
}

Try to change your code and achieve all the possible color combinations!

The code:

int redPin = 11;
int greenPin = 10;
int bluePin = 9;
int i;
void setup() {
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop() {
// The red light on
analogWrite(redPin, 255);
analogWrite(greenPin, 0);
analogWrite(bluePin, 0);
delay(2000);
// The green light on
analogWrite(redPin, 0);
analogWrite(greenPin, 255);
analogWrite(bluePin, 0);
delay(2000);
// The blue light on
analogWrite(redPin, 0);
analogWrite(greenPin, 0);
analogWrite(bluePin, 255);
delay(2000);
}

 

 

Making an RGB Led

In this example we will use three LEDs, one red, one green and one blue, trying to simulate an RGB led, and then we will program this to get a range of colors.

For this task we will need three color LEDs (red, green, blue) and three resistors, one for each color. The connection is shown in Figure 27. Note which pin you will connect with which color. For all colors we will need PWM ports rather than simple digital ones. Also, place the leds as close as possible so that we can then cover them with a cylindrical tube to see what color is coming out!

Piezo-Buzzers

For this work we will need to connect our leds to PWM ports. Our approach is the same as the previous one, and the same steps you should take here. The difference is that you made the RGB led yourself! To see the mix of colors, you'll use a cylindrical white tube, open from both edges, so you can cover the Led and you can see from the top edge what's going on.

1. Make the necessary initializations (variable statements, values, input / output).
2. With a repetitive process you will gradually give values ​​to each led, as in the previous example.
Try to change your code and achieve all the possible color combinations!

The code:

int redPin = 11;
int greenPin = 10;
int bluePin = 9;
int i;
void setup() {
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop() {
// The red light on
analogWrite(redPin, 255);
analogWrite(greenPin, 0);
analogWrite(bluePin, 0);
delay(2000);
// The green light on
analogWrite(redPin, 0);
analogWrite(greenPin, 255);
analogWrite(bluePin, 0);
delay(2000);
// The blue light on
analogWrite(redPin, 0);
analogWrite(greenPin, 0);
analogWrite(bluePin, 255);
delay(2000);
}

 

 

Traffic lights

In this example we will simulate the operation of the lanterns. Thus, by giving appropriate intervals, we will keep each color open for some time, and then the process will be repeated from the beginning. Lanterns will flash as in Greece *, ie Green, Orange, Red, Green, and so on.

For this task we will need three LEDs, one red, one green and one orange (or yellow) - one for each traffic light, as well as the corresponding resistors. You can see the circuit in Figure 28 below. Note that because there are not many GND sockets, all are shorted to the breadboard (-) and we connect the GND of Arduino.

Piezo-Buzzers

All that remains is to program our circuit according to what we have already mentioned. That is, you will make the necessary initializations and then:

1. You should give a voltage of 5V to the led you want to light up and 0V to the rest.
2. We will have to wait for 1 second during which obviously a LED lights up and the rest is off.
3. Steps 1, 2 will do alternately for red, orange and green LEDs.
4. Try adjusting times. Orange usually lights for much less time than the other two colors.

The code:

int ledRed = 11;
int ledOrange = 10;
int ledGreen = 9;
void setup() {
pinMode(ledRed, OUTPUT);
pinMode(ledOrange, OUTPUT);
pinMode(ledGreen, OUTPUT);
}
void loop() {
// red for 3 seconds
digitalWrite(ledRed, HIGH);
digitalWrite(ledOrange, LOW);
digitalWrite(ledGreen, LOW);
delay(3000);
// green for 5 seconds
digitalWrite(ledRed, LOW);
digitalWrite(ledOrange, LOW);
digitalWrite(ledGreen, HIGH);
delay(5000);
// orange for 1 second
digitalWrite(ledRed, LOW);
digitalWrite(ledOrange, HIGH);
digitalWrite(ledGreen, LOW);
delay(1000);
}

 

 

Pedestrian traffic lights

In this example we will enrich the operation of the lanterns, as implemented in example 8, with two more traffic lights for pedestrians. Thus, by giving appropriate time intervals, we will keep each traffic color open for some time, and when there is traffic, the red pedestrian will light up, and when there is red in traffic, the pedestrian green will light up. Then the process will be repeated from the beginning.

For this work, we will need, in addition to those in worksheet 8, two leds, one red and one green to simulate the operation of pedestrian lights and the corresponding resistors. You can see the circuit in Figure 29 below. Note again that the ground GND has been shorted to the breadboard.

Piezo-Buzzers

All that remains is to program our circuit according to what we have already mentioned. You should specify the times and combinations of pedestrian and traffic lights.

The code:

int ledRed = 11;
int ledOrange = 10;
int ledGreen = 9;
int pedRed = 4;
int pedGreen = 3;
void setup() {
pinMode(ledRed, OUTPUT);
pinMode(ledOrange, OUTPUT);
pinMode(ledGreen, OUTPUT);
pinMode(pedRed, OUTPUT);
pinMode(pedGreen, OUTPUT);
}
void loop() {
// red for 3 seconds and green to pedestrians
digitalWrite(ledRed, HIGH);
digitalWrite(ledOrange, LOW);
digitalWrite(ledGreen, LOW);
digitalWrite(pedRed, LOW);
digitalWrite(pedGreen, HIGH);
delay(3000);
// red to pedestrians - wait 1 second - green to cars
digitalWrite(pedRed, HIGH);
digitalWrite(pedGreen, LOW);
delay(1000);
// Green to cars for 5 seconds
digitalWrite(ledRed, LOW);
digitalWrite(ledOrange, LOW);
digitalWrite(ledGreen, HIGH);
delay(5000);
// orange for 1 second
digitalWrite(ledRed, LOW);
digitalWrite(ledOrange, HIGH);
digitalWrite(ledGreen, LOW);
delay(1000);
}

 

 


Pedestrian traffic lights and stop button

In this example we will further enrich the operation of the lanterns, as has been done in the previous examples (sheets 8 and 9), with the introduction of a button that will determine the operation of the lanterns as follows: normally the traffic light will be permanently open Green), unless the key is pressed, which means that a pedestrian is asking to go. In this case the traffic (orange and red) should be interrupted and the pedestrian green switched on for a specified time. Finally, the pedestrian traffic light will turn red again and the car traffic will start again (green). Then the process will be repeated from the beginning.

For this work, we will need, in addition to worksheet materials 8 and 9, a button to simulate the operation of the pedestrian button. We will also need a resistor for the button. You can see the circuit in Figure below.

Piezo-Buzzers

All that remains is to program our circuit. For convenience, you are given instructions in the form of steps:

1. Make the necessary initializations (variables, values, input / output).
2. Determine the times and combinations of pedestrian and traffic lights.
3. You should read the input from the corresponding pin button and when you are detecting input, perform the corresponding actions described above. For this step, it is suggested to use time logging to know when the button was last pressed using the millis () function. Also set a minimum amount of time when the pedestrian green will not light up. Note which steps should be taken only once and which will be repeated at all times.

The code:

int ledRed = 11;
int ledOrange = 10;
int ledGreen = 9;
int pedRed = 4;
int pedGreen = 3;
int btn = 5; //connection of button to pin 5
int lastPedPass = 0; // when the traffic was paused for pedestrian traffic
int btnState; //to see if the button is pressed
void setup() {
pinMode(ledRed, OUTPUT);
pinMode(ledOrange, OUTPUT);
pinMode(ledGreen, OUTPUT);
pinMode(pedRed, OUTPUT);
pinMode(pedGreen, OUTPUT);
pinMode(btn, INPUT);
// Red on the pedestrians and green on the cars initially
digitalWrite(pedRed, HIGH);
digitalWrite(pedGreen, LOW);
digitalWrite(ledRed, LOW);
digitalWrite(ledOrange, LOW);
digitalWrite(ledGreen, HIGH);
}
void loop() {
int btnState = digitalRead(btn);
if (btnState == HIGH && (millis() - lastPedPass) > 5000) {
// If the button is pressed and the time since the last change is over 5 seconds
// orange for 1 second
digitalWrite(ledRed, LOW);
digitalWrite(ledOrange, HIGH);
digitalWrite(ledGreen, LOW);
delay(1000);
// red in the car for 5 seconds, waiting 1 sec and green on the pedestrians
digitalWrite(ledRed, HIGH);
digitalWrite(ledOrange, LOW);
digitalWrite(ledGreen, LOW);
delay(1000);
digitalWrite(pedRed, LOW);
digitalWrite(pedGreen, HIGH);
delay(3000);
// red on the pedestrians - wait for 1 second - and then green in the cars
digitalWrite(pedRed, HIGH);
digitalWrite(pedGreen, LOW);
delay(1000);
// green for cars for 5 seconds
digitalWrite(ledRed, LOW);
digitalWrite(ledOrange, LOW);
digitalWrite(ledGreen, HIGH);
lastPedPass = millis(); // keep the time of the last change of lights
}
}

 

 

Continue to Examples page 2