0% found this document useful (0 votes)
2 views46 pages

presentation arduino

The document provides a comprehensive introduction to Arduino, covering its architecture, software installation, programming basics, and examples of connecting devices such as LEDs and ultrasonic sensors. It includes practical exercises for controlling traffic lights and DC motors, as well as guidance on building a mobile robot project. Key concepts such as logic ports, PWM, and the use of resistors are also discussed to enhance understanding of Arduino functionality.

Uploaded by

courau.antoine
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
2 views46 pages

presentation arduino

The document provides a comprehensive introduction to Arduino, covering its architecture, software installation, programming basics, and examples of connecting devices such as LEDs and ultrasonic sensors. It includes practical exercises for controlling traffic lights and DC motors, as well as guidance on building a mobile robot project. Key concepts such as logic ports, PWM, and the use of resistors are also discussed to enhance understanding of Arduino functionality.

Uploaded by

courau.antoine
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 46

Prise en main des

cartes arduino
Discovering Arduino

Arduino is an open-source electronic programmable platform with input and output ports.
Arduino cards are available in various sizes.

Arduino Uno Arduino Mega Arduino Nano


Architecture of an Arduino card
Logic and pwm ports

• Logic : Two states input or output port – 0 or 5V

• PWM (pulse width modulation) : logic output which can change very quickly (500Hz) and
simulate a variable output between 0 and 5V. The mean value depend on the ratio of

5V 5V V=5 t1/t2

0V 0V
t1 t2
5V 5V
V=5 t1/t2

0V 0V
t1 t2
Installing Arduino software
• Download the software from https://www.arduino.cc/en/Main/Software
• Then install
Using Arduino software
1. Launch the software
2. Connect the Arduino card to the computer using the USB cable
3. Select the card type (Tools\Board)
Using Arduino software
4. Select the communication port (Tools\Port)
5. Write the program (see next slide)
6. Save the program ( File\Save as)
7. Transfer the program to the card (Skecth\Upload or the right arrow)
8. Test and enjoy!
Program structure

• Arduino language is very similar to C


• It is based on functions
• A function is a group of line code with a unique name, a goal and which sometime returns some data.
• A function follows the syntax :
Function’s arguments

• The minimal program in arduino contains 2 functions :


setup and loop (which return nothing)

setup is run once at the beginning of the program


loop will run repeatedly
Connecting devices to an Arduino card
Connecting devices to an Arduino card

How to turn on a led


1. Declare on which pin the LED is connected and how you will use this pin

void setup() {

pinMode(13, OUTPUT);

2. Declare what action you will do repeatedly

void loop() {

digitalWrite(13, HIGH);

}
Second code example: the blinking led
Second code example: the blinking led
void setup() {
Comment: won’t
// initialize digital pin 13 as an output. be executed by the
program
pinMode(13, OUTPUT);
220 Ω
}

// the loop function runs over and over again forever


void loop() {
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(13, LOW);
// turn the LED off by making the voltage LOW delay(1000);
// wait for a second}
You can find this code in the software,
Your turn: simple traffic lights
1st exercise: the lights will turn green to yellow to red with a specified delay (10s
green, 2 sec yellow and 7 sec red).
Your turn: simple traffic lights (solution)
Your turn: simple traffic lights (solution)
int greenLED= 12; //the code will flash the LED connected to pin 13 Give a name (and a type) to some variables
int yellowLED= 11; //the yellow LED is connected to digital pin 11 to be able to use them again and to clarify
int redLED= 10; //the red LED is connected to digital pin 10 the code

void setup() {
//makes all the LEDs outputs
pinMode (greenLED, OUTPUT);
pinMode (yellowLED, OUTPUT); Set the type of the ports
pinMode (redLED, OUTPUT);
}

http://www.learningaboutelectronics.com/Articles/Arduino-traffic-light-circuit.php
Your turn: simple traffic lights (solution)
void loop(){
digitalWrite (greenLED, HIGH);//turns green LED on
digitalWrite (yellowLED, LOW);//turns yellow LED off
digitalWrite (redLED, LOW);//turns red LED off
delay(15000);

digitalWrite (greenLED, LOW); //turns green LED off


digitalWrite (yellowLED, HIGH);//turns yellow LED on
digitalWrite (redLED, LOW);//turns red LED off
delay(2000);

digitalWrite (greenLED, LOW); //turns green LED off


digitalWrite (yellowLED, LOW);//turns yellow LED off
digitalWrite (redLED, HIGH);//turns red LED on
delay(15000);
}
Your turn: simple traffic lights (2nd version)

• 2nd exercise: the light stays green until a pedestrian wants


to cross and push a button. Then, after 2s the light turn
yellow (2s), then red (8 sec), and finally green again.

Note 1: If …. Else
Note 2: how to read a digital input
if (condition1) {
digitalRead(button)
// do Thing A
}
Example :
else if (condition2) {
if digitalRead(button) == HIGH
// do Thing B
}
else {
// do Thing C
}
Note: pull-up and pull down resistance
• When connecting a switch to an electronic device, it is advised to add a pull-up or pull-
down resistor.

No resistor, when the


switch is open, the wire is Pull-up resistor Pull-down resistor
floating

• The usual value for Arduino is 10 kΩ.


• Arduino cards have a built-in pull –up resistor that can be activated with
pinMode(8, INPUT_PULLUP)
Connecting devices to an Arduino card
SWITCHES
Your turn: simple traffic lights (2nd version) - solution
Your turn: simple traffic lights (2nd version) - solution
int red = 3;
int yellow = 4;
int green = 5;
int button = 7;

void setup() {
pinMode(red, OUTPUT);
pinMode(yellow, OUTPUT);
pinMode(green, OUTPUT);
pinMode(button, INPUT);
digitalWrite(green, HIGH);
digitalWrite(red, LOW);
digitalWrite(yellow, LOW);
}
Your turn: simple traffic lights (2nd version) - solution
if (digitalRead(button) == HIGH){
delay(15); // software debounce
if (digitalRead(button) == HIGH) {
// if the switch is HIGH, ie. pushed down - change the lights!
changeLights();
delay(10000);
}
}
Your turn: simple traffic lights (2nd version) - solution
void changeLights(){
// green off, yellow on for 3 seconds
digitalWrite(green, LOW);
digitalWrite(yellow, HIGH);
digitalWrite(red, LOW);
delay(3000);
// turn off yellow, then turn red on for 5 seconds
digitalWrite(green, LOW);
digitalWrite(yellow, LOW);
digitalWrite(red, HIGH);
delay(4000);
// red and yellow on for 2 seconds (red is already on though)
digitalWrite(green, LOW);
digitalWrite(yellow, HIGH);
digitalWrite(red, HIGH);
delay(2000);
// turn off red and yellow, then turn on green
digitalWrite(green, HIGH);
digitalWrite(yellow, LOW);
digitalWrite(red, LOW);
delay(3000);
}
How to print an analog value to a numeric output

analogWrite(PinNumber, ledValue);

PinNumber must be a PWM output


How to control a LED with a potentiometer
How to control a LED with a potentiometer

int potPin = A0;


int ledPin = 11;
float potValue = 0;
int ledValue = 0;

void setup()
{
Serial.begin(9600);
pinMode (potPin , INPUT);
pinMode (ledPin , OUTPUT);
}
How to control a LED with a potentiometer
void loop()
{
potValue=analogRead(potPin);
ledValue=int(potValue/1023*255)
analogWrite(ledPin, ledValue);
Serial.print("Valeur du potentiomètre = ");
Serial.println(potValue);
Serial.print("Valeur de la led = ");
Serial.println(ledValue);
Serial.print ("\n");
delay(500);
}
The ultra sonic detector VMA 306
1st step:
Find information on the internet on how to use an ultrasonic detector.

Attention au câblage ! Lire le nom des bornes sur le capteur


The ultra sonic detector VMA 306
• How it works
It emits an ultrasound at 40 000 Hz which travels through the air and if there is
an object or obstacle on its path It will bounce back to the module. Considering
the travel time and the speed of the sound you can calculate the distance.
The ultra sonic detector VMA 306
• How to connect the sensor
The VMA 306 Ultrasonic Module has 5 pins, Ground, VCC, Out,Trig and Echo. Out will not
be used.
The Ground and the VCC pins of the module needs to be connected to the Ground and the
5 volts pins on the Arduino Board respectively and the trig and echo pins to any Digital I/O
pin on the Arduino Board.

Attention au
câblage ! Lire le
nom des bornes
sur le capteur
The ultra sonic detector VMA 306
• How to know how far is the obstacle
In order to generate the ultrasound you need to set the Trig on a High State for 10 µs. That
will send out an 8 cycle sonic burst which will travel at the speed sound and it will be
received in the Echo pin. The Echo pin will output the time in microseconds the sound
wave traveled.
The ultra sonic detector VMA 306
• How to know how far is the obstacle
For example, if the object is 10 cm away from the sensor, and the speed of the sound is
340 m/s or 0.034 cm/µs the sound wave will need to travel about 294 u seconds. But what
you will get from the Echo pin will be double that number because the sound wave needs
to travel forward and bounce backward. So in order to get the distance in cm we need to
multiply the received travel time value from the echo pin by 0.034 and divide it by 2.
The ultra sonic detector VMA 306
// defines pins numbers // Reads the echoPin, returns the sound wave
const int trigPin = 9; travel time in microseconds
const int echoPin = 10; duration = pulseIn(echoPin, HIGH);
// defines variables
long duration; // Calculating the distance
int distance; distance= duration*0.034/2;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output // Prints the distance on the Serial Monitor
pinMode(echoPin, INPUT); // Sets the echoPin as an Input Serial.print("Distance: ");
Serial.begin(9600); // Starts the serial communication Serial.println(distance);
} }

void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2); Note: this example also introduce the use of
the serial communication.
// Sets the trigPin on HIGH state for 10 micro seconds You can see the result using the serial monitor
digitalWrite(trigPin, HIGH); of the software (Tools \ serial monitor)
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
DC motors control

Permanent magnets

Collector

Collector
DC motor with arduino
DO NOT PLUG THE MOTOR DIRECTLY TO THE ARDUINO BOARD

You could damage the card


Instead, use a DC motor drive
Such as a L298 bridge
DC motor with arduino

https://www.gotronic.fr/pj2-sbc-motodriver2-manual-2509.pdf
How to use L298 with one DC motor
https://www.gotronic.fr/pj2-sbc-motodriver2-manual-2509.pdf

https://www.instructables.com/id/How-to-Use-L298n-to-Control-Dc-Motor-
With-Arduino/

int in1 = 9;
int in2 = 8;
void setup() {
pinMode(in1, OUTPUT); //Declaring the pin modes, obviously they're outputs
pinMode(in2, OUTPUT);
}
How to use L298 with one DC motor

void TurnMotorA(){ void loop() {


digitalWrite(in1, HIGH); TurnMotorA();
digitalWrite(in2, LOW); delay(3000);
} TurnOFFA();
delay(2000);
void TurnOFFA(){ }
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
}
How to change the direction

Add a new function void loop() {


void TurnMotorA2() { TurnMotorA();
digitalWrite(in1,LOW); delay(3000);
digitalWrite(in2,HIGH); TurnOFFA();
} delay(2000);
TurnMotorA2();
delay(3000);
TurnOFFA();
delay(2000);
}
How to change the speed
How to change the speed
int in1 = 9; void TurnOFFA(){
digitalWrite(in1, LOW);
int in2 = 8; digitalWrite(in2, LOW);
int ConA = 10; }
analogWrite(ConA,0);

void setup() { void TurnMotorA2(){


digitalWrite(in1, LOW);
pinMode(in1, OUTPUT); digitalWrite(in2, HIGH);
pinMode(in2, OUTPUT); analogWrite(ConA,250);
}
pinMode(ConA, OUTPUT);
} void loop() {
TurnMotorA();
delay(2000);
TurnOFFA();
void TurnMotorA(){ delay(2000);
digitalWrite(in1, LOW); TurnMotorA2();
delay(4000);
digitalWrite(in2, HIGH);
TurnOFFA();
analogWrite(ConA,100); delay(2000);
}
}
Projet – Robot mobile
Objectif : réaliser un suivi d’objet, dans une direction uniquement, à une distance fixée
1ere étape :

• Fixer le détecteur à ultrason sur le chassis du robot dans la rainure avec les vis
de petit diamètre, écrous et rondelles
• Installer la carte Arduino sur son support transparent (les trous sont référencés
1)
• Installer le support de la carte sur le robot
Etapes suivante
• Réaliser le câblage de la carte arduino
• Ecrire et tester le programme (lors des tests la carte arduino pourra être
alimentée par les piles (prévoir un fil qui ira de l’alimentation de carte de
pilotage des moteurs à la borne in de la carte). Débrancher cette alimentation
pendant le téléversement.
Défi supplémentaire
Rajouter un potentiomètre pour pouvoir piloter en temps réel la consigne de
distance (entre 5 et 35 cm).

A la fin du projet retirer les câbles qui relient la carte Arduino au détecteur et à la
carte de commande du moteur sauf la masse et l’alim.

You might also like