presentation arduino
presentation arduino
cartes arduino
Discovering Arduino
Arduino is an open-source electronic programmable platform with input and output ports.
Arduino cards are available in various sizes.
• 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
void setup() {
pinMode(13, OUTPUT);
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 Ω
}
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);
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.
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);
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 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
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
• 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.