Vous êtes sur la page 1sur 27

[Basic Lab]

Basic experiment 2.1


Blinking an LED with Arduino
Basic experiment 2.2
Press a switch to change the state of an LED
Writing a simple program with INPUT/OUTPUT
Basic experiment 2.3
Toggle an LED using a switch
Writing a simple program with internal functions

[Basic Lab]
Basic experiment 2.4
Create a program to adjust the LED brightness with PWM
Basic experiment 2.5
Verify the actual value of a variable resistor connected to
an analog input from the PC GUI using the serial monitor
Basic experiment 2.6
Change the rotation direction and speed of a motor
based on sensor input
[Task Lab]
Challenge 2
Control a motor with advanced conditional processing

Basic experiment 2.1

Blinking an LED with Arduino


Installation of the IDE (Integrated
Development Environment)

How to use the Arduino IDE


Create a new sketch
Build a sketch
Run a program: transfer
and execution

Download
http://arduino.cc

Choose the right


version for your
environment

How to use the Arduino IDE


Compile the program

Upload the compiled sketch on the board

Text editor

Write a new sketch

Open an existing or an example sketch

Save the current sketch


Message area
Open the serial monitor
Text area

Upload the sketch on Arduino


1. Connect Arduino to the PC with a USB cable
2. Start the Arduino IDE
3. Choose from the Menu the blink example program
File->Examples->1.Basics/Blink
4. Open Tools->Board and select the connected
Arduino board

Windows Check on the Device Manager which


port is assigned to the Arduino board

Mac Select /dev/tty.usbmodem-???

5. Press the upload button

Arduino program structure


Setup function
It is called only once at startup
Contains initialization settings,
e.g. the operation mode of pins

main()
setup()

Loop function
Called repeatedly
Contains the main control
process that never stops
Main function is prepared by the
Arduino IDE

loop()

Basic experiment 2.2

Press a switch to change the state of an LED


5V
D2

How to use Digital IO


void pinMode(pin, mode)
void digitalWrite(pin, value)
int digitalRead(pin)

D12

GND

Digital IO

Set the operational mode of a pin

Set the output value of the pin

void pinMode(pin, mode)

void digitalWrite(pin, value)

Read an input value from a pin (returned as integer)

pin:

int digitalRead(pin)
pin number 2 13, A0 A5

mode: operational mode 0 if input, 1 if output


value: output mode: Low(0)/High(1)
input mode with built-in pull-up: On(1)/Off(0)

Connecting a switch
PULL-UP

PULL-DOWN
I (current)

Open circuit:
No current
5V
Direct
connection

OPEN SWITCH CLOSED SWITCH

I (current)

Direct
connection

GND (0V)
CLOSE

NO PULL RESISTOR

GND

I (current)

CLOSE
5V

Open circuit:
No current

OPEN SWITCH CLOSED SWITCH

Direct
connection

CLOSE

SHORTCIRCUIT!!!

When not using the internal pull-up of Arduino use


either a pull-down or pull-up resistor

Pull-Up: the pin value is high (1). Closing the switch


will cause the pin to go low (0)

Pull-Down: the pin value is low (0). Closing the


switch will cause the pin to go high (1)

If no pull-up or pull-down resistors are in place,


pressing the switch will SHORTCIRCUIT the power
source to ground. BE CAREFUL!!!!

Basic
experiment
2.2

const int buttonPin = 2; // pin connected to the push button


const int ledPin = 12;
// pin connected to LED
int buttonState = 0; // variable to store the push button status
void setup ( ) {
// initialize the LED pin as an output
pinMode ( ledPin, OUTPUT);
// initialize the push button pin as an input
pinMode ( buttonPin, INPUT);
}
void loop ( ) {
// read the state of the push button
buttonState = digitalRead ( buttonPin );
// check if the button is pressed (closed)
// if it is, buttonState is LOW (0)
if ( buttonState == LOW ) {
// turn LED on
digitalWrite ( ledPin, HIGH );
} else {
digitalWrite ( ledPin, LOW );
}
}

Basic experiment 2.3

Toggle an LED using a switch


5V

Switch chattering

D2

D12

GND

Switch chattering
Due to manual operation of the switch, during transitions
the voltage takes some time to stabilize
Hardware solution
High pass filter circuit
Software solution
Ignore the high frequency transitions (chatter) for
a set time (delay) software high pass filter
Vin [V]
High frequency transitions (chatter)

Switch pressed

t [s]

Basic
experiment
2.3

const int buttonPin = 2;


const int ledPin = 12;
int buttonState = HIGH;
int ledState = LOW;
void setup () {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
digitalWrite(buttonPin, HIGH);
}
void loop () {
int state = digitalRead(buttonPin);
if (buttonState == HIGH && state == LOW) { // transition
SOFTWARE FILTER
// toggle LED
delay(50); // wait for a set time
if (ledState == LOW) {
state = digitalRead(buttonPin); // read the pin
ledState = HIGH;
// toggle LED
} else {
if (state == LOW) {
ledState = LOW;
if (ledState == LOW) {
}
ledState = HIGH;
}
} else {
buttonState = state;
ledState = LOW;
digitalWrite(ledPin, ledState);
}
}
}

Basic experiment 2.4

Create a program to adjust the LED


brightness with PWM
What is PWM
(Pulse Width Modulation)
D9

Use of Analog IO
void analogWrite(pin, value)
int analogRead(pin)

GND

Analog IO

Output of a PWM signal

void analogWrite(pin, value)

pin: pin number (3,5,6,9,10,11)

value: PWM duty cycle (0 255)

Read the voltage input (integer value, digital conversion)

int analogRead(pin)

Pin: analog pin number (a0 a5

Returned value x internal ADC 10 bit (0 1023

GND (0V) 0
Corresponding analog voltage

Positive (5V) 1023

Voltage v[V]: x = 5(V): 1023

Returned value

PWM Pulse Width Modulation

PWM is a method to control continuous power output


varying the pulse width ratio of a switching signal over a
fixed period
pulse HIGH: Thigh [s]
pulse LOW: Tlow [s]
period T = Thigh + Tlow [s]
If the period T is short enough it does not affect
significantly the LED, which will see a continuous power
output proportional to Thigh/T
V [V]

This Thigh/T is called Duty Vcycle


[V]
Continuous
input to load

Continuous
input to load

Basic experiment 2.4


void setup () {
pinMode(9, OUTPUT);
}
void loop () {
// output HIGH for 5 sec
digitalWrite(9, HIGH);
delay(5);
// output LOW for 5 sec
digitalWrite(9, LOW);
delay(5);
}

Basic PWM implementation:


Ignoring the time needed for pin output transition
(usually s), I can switch the pin every 5[s].
However, delays will stop the whole processing
for 5[s], wasting CPU time that can be used for
other operations.
SOLUTION:
use an internal timer to separate timekeeping
and actual processing.

Embedded PWM:
Arduino has pins with embedded PWM
function. Those pins can automatically
toggle with a duty cycle that can be
programmed. Those pins are marked on
the board with ~.

void setup () {
pinMode(9, OUTPUT);
analogWrite(9, 127);
}
void loop () {
}

Basic experiment 2.5

Verify the actual value of a variable resistor


connected to an analog input from the PC
GUI using the serial monitor
5V

Serial communication
Analog IO
void analogWrite(pin, value)
int analogRead(pin)

A3

GND

Serial monitor

It allows communication between Arduino and the PC

Serial monitor
startup button

Serial monitor

Mu
st
b

et

he

sa

me

Serial
communication
speed setting

Serial communication

Setting the communication speed

Serial.begin(speed)

Speed unit: Baud rate [bps]


(9600bps, 19200bps, 38400bps)

Set only once in the Setup function

Send data to PC

Serial.print(val) continuous printing

Serial.println(val)

line printing

val: value sent to PC any type

Println starts a new line for each transmission

Basic experiment 2.5

int data;
void setup () {
Serial.begin(9600);
}
void loop () {
// read and convert analog voltage input
data = analogRead(A3);
CHALLENGE:
Serial.print(Data is );
This code sends a digital value that
// print digital converted input
has no conceptual meaning. What is
Serial.println(data);
the value of the input in Volts?
delay(1000);
Write a program to send the correct
}
input in Volts [V]

Basic experiment 2.6

Change the rotation direction and speed of


a motor based on sensor input

Power
supply
USB
Motor

H-bridge driver board

Sensor

Basic experiment 2.6

Change the rotation direction and speed of


a motor based on sensor input

H-bridge driver board schematic

void setup()
{
Basic
experiment
2.6
pinMode(pwm1Pin, OUTPUT);
pinMode(direction1Pin, OUTPUT);
pinMode(sensor1Pin, INPUT);
analogWrite(pwm1Pin, 0);
}
void setMotorSpeed(void) {
int val = analogRead(analogPin);
int speed = val >> 2;
analogWrite(pwm1Pin, speed);
}
void setMotorDirection(void) {
int direction;
if (!digitalRead(sensor1Pin)) {
direction = 1;
} else {
direction = 0;
}
digitalWrite(direction1Pin, direction);
}
void loop () {

Challenge 2

Control a motor with advanced conditional


processing
Sensor input analysis

Processing based on the sensor input


Update the motor output as needed

Challenge 2

Program specifications
input/output
Digital input: CN01 (touch sensor: pin15)
Output: MOTOR_1 (direction: pin12, speed: pin3)

When the touch sensor is ON, count UP
When count exceeds "9", count variable is reset to "0",
and the motor rotates for a predetermined time
Hardware Configuration: same as basic experiment 2.6
To check the touch sensor output signal, study the signal with an
oscilloscope first.
The touch sensor is supposed to be touched, hold, and then briskly
released after a short time

Vous aimerez peut-être aussi