Académique Documents
Professionnel Documents
Culture Documents
[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
Download
http://arduino.cc
Text editor
main()
setup()
Loop function
Called repeatedly
Contains the main control
process that never stops
Main function is prepared by the
Arduino IDE
loop()
D12
GND
Digital IO
pin:
int digitalRead(pin)
pin number 2 13, A0 A5
Connecting a switch
PULL-UP
PULL-DOWN
I (current)
Open circuit:
No current
5V
Direct
connection
I (current)
Direct
connection
GND (0V)
CLOSE
NO PULL RESISTOR
GND
I (current)
CLOSE
5V
Open circuit:
No current
Direct
connection
CLOSE
SHORTCIRCUIT!!!
Basic
experiment
2.2
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
Use of Analog IO
void analogWrite(pin, value)
int analogRead(pin)
GND
Analog IO
int analogRead(pin)
GND (0V) 0
Corresponding analog voltage
Returned value
Continuous
input to load
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 () {
}
Serial communication
Analog IO
void analogWrite(pin, value)
int analogRead(pin)
A3
GND
Serial monitor
Serial monitor
startup button
Serial monitor
Mu
st
b
et
he
sa
me
Serial
communication
speed setting
Serial communication
Serial.begin(speed)
Send data to PC
Serial.println(val)
line printing
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]
Power
supply
USB
Motor
Sensor
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
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