Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
1.5V Battery
–
Current
If we connect a conductor the potential difference, it
will create a current from High Voltage to Low
Voltage. Simulation
Current
+
1.5V Battery
Conductor
–
Ohm’s Law
There is one very very important relationship
between Voltage, Current and Resistance
V=IxR
Current I
Resistance
Voltage V R
–
What is Arduino ?
• Arduino is an open-source electronics platform
based on easy-to-use hardware and software. It's
intended for anyone making interactive projects.
• https://www.arduino.cc/
Different Versions of
Arduino ?
• From the official site, Arduino have 4 different
levels of boards
• https://www.arduino.cc/en/Main/Products
Arduino UNO
UNO Pinout
Arduino IDE
• An IDE (Integrated Development Environment) is
a software that allows you to write code and
upload it to the board.
• The Arduino Software (IDE) is free and can be
downloaded at https://www.arduino.cc/
• It runs on Windows, Mac OS X, and Linux. The
environment is written in Java and based on
Processing and other open-source software.
Arduino IDE
Arduino IDE
Arduino IDE
• After installed the software, there will be a
shortcut on the desktop, click to invoke the
IDE and there is a sketch window will show up.
Arduino IDE
• Make sure you choose the UNO for the board
under Tools
Sketch of Arduino
• A sketch is the name that Arduino uses for a
program. It's the unit of code that is uploaded to
and run on an Arduino board.
• The sketch is in .ino format
• There are two special functions that are a part of
every Arduino sketch:
• setup() and loop().
setup() and loop()
void setup() {
/* The setup() is called once, when the sketch starts. It's a
good place to do setup tasks like setting pin modes or
initializing libraries. */
}
void loop() {
/* The loop() function is called over and over and is heart
of most sketches. */
}
void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
void loop() {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
Upload Your code to the
Board
void loop() {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
Loop()
Please note that the program inside the loop() is
running very fast. Typically in term of 106 (i.e.
Mega) Hz. It would be difficult for human or other
devices to see and feel.
Timing
If we use a timing diagram to represent the LED
project
time
1 1 1 1 1 1
second second second second second second
Change your program
/* Turns on an LED on for one second, then off for one second, repeatedly.
void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
void loop() {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1); // wait for a second
time
1 1 1 1 1 1
second second second second second second
Timing
Theoretically, the timing diagram is like this.
Can your eye see this ?
time
0.001 0.001 0.001 0.001 0.001 0.001
second second second second second second
Timing
The LED cannot response that fast, also,
Your eyes cannot blink on and off for 1000 times a
second, so, to your eye, the LED is like this
time
0.001 0.001 0.001 0.001 0.001 0.001
second second second second second second
What’s more
We define few more things in this figure
time
0.001 0.001 0.001 0.001 0.001 0.001
second second second second second second
PERIOD
Period and Frequency
From the previous figure
HIGHTIME + LOWTIME = PERIOD
PERIOD means time for one cycle
ଵ
Frequency =
ாோூை
void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
}
void loop() {
analogWrite(led, 120); // any value between 0 to 255
// 0 will be OFF, 255 will be full ON
}
Question
Can you test the function using the LED on the
board ? Why ?
Light-Emitting Diode
(LED)
Resistor Color Code
LED Connection
Female Female
Female
–
+
Female
Male
Male
LED Connection
+
LED Connection
–
GND Pin 11
+
Basic Programming
Control Statements
For most of the time, when we run our program, we
need to make decisions. We will introduce two most
useful statements
1. if statement
2. for statement
If statement
The if() statement is the most basic of all
programming control structures. It allows you
to make something happen or not, depending
on whether a given condition is true or not. It
looks like this:
if (someCondition) {
// do stuff if the condition is true
}
void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
Serial.begin(9600);
digitalWrite(led, LOW);
void loop() {
if (Serial.available() > 0) {
char input = Serial.read();
if (input == ‘A’) {
digitalWrite(led, HIGH);
} else if (input == ‘B’) {
digitalWrite(led, LOW);
}
}
}
Upload Your code to the
Board
Choose Serial
Monitor
Upload Your code to the
Board
M
It will move when we apply voltage across the
terminals
Motor Movement
+ M -
? M ?
Basic Car Control
Now we are going to use the concept developed to
control a car.
(Front) Our Car (Back)
How to make the Car move
?
From the above picture
1. How many motors are there in the Car
2. If I want the car to go forward, how can I control
the motor ?
Motor Control Pin
The 4 pins of the motors are connected to the
following Arduino pins
Forward
void setup()
{
// Initialize all I/O
pinMode(Left_motor_go,OUTPUT); // PIN 9 (PWM)
pinMode(Left_motor_back,OUTPUT); // PIN 8 (NO PWM)
pinMode(Right_motor_go,OUTPUT); // PIN 10 (PWM)
pinMode(Right_motor_back,OUTPUT); // PIN 11 (PWM)
digitalWrite(Right_motor_go,LOW); // Right Motor Stop
digitalWrite(Right_motor_back,LOW);
digitalWrite(Left_motor_go,LOW); // Left Motor Stop
digitalWrite(Left_motor_back,LOW);
}
Motor sketch
void forward() //
{
digitalWrite(Right_motor_go,HIGH); // Right Motor Forward
digitalWrite(Right_motor_back,LOW);
digitalWrite(Left_motor_go,HIGH); // Left Motor Forward
digitalWrite(Left_motor_back,LOW);
}
void brake()
{
digitalWrite(Right_motor_go,LOW); // Right Motor Stop
digitalWrite(Right_motor_back,LOW);
digitalWrite(Left_motor_go,LOW); // Left Motor Stop
digitalWrite(Left_motor_back,LOW);
}
void loop()
{
forward(); // forward
delay(2000); // delay for 1 second
brake(); // stop
delay(1000); // delay for 1 second
}
Motor Control Turn
If I want the car to Turn Left ? Turn Right ?
Left
Turn
Right
Turn
Motor Control Turn
Can we make Turn Left and turn Right faster ? (i.e.
Spin ? )
Left Spin
Right
Spin
Motor Sketch modified
Try to implement the function such that the car can
1. Go forward for 1 second
2. Go backward for 1 second
3. Turn Left for 3 seconds
4. Turn Right for 3 seconds
5. Spin Left for 2 seconds
6. Spring Right for 2 seconds
7. Stop for 3 seconds
Motor Control Turn
Pin 9 and Pin 10 had PWM function. Can you think
of how to use PWM in order for the car to turn ? (NO
PWM function on Pin 8)
Remember than PWM will average the voltage. If
smaller the voltage, the motor will turn slower.
Motor Control Turn
What will happen to the following code ?
analogWrite(Right_motor_go,50);
digitalWrite(Right_motor_back,LOW);
analogWrite(Left_motor_go,200);
digitalWrite(Left_motor_back,LOW);
Sensor
At the front of the Car, there is a pair of infra red
sensor to detect if there is an obstacle in front.
void setup()
{
// Initialize all I/O
pinMode(Left_motor_go,OUTPUT); // PIN 9 (PWM)
pinMode(Left_motor_back,OUTPUT); // PIN 8 (NO PWM)
pinMode(Right_motor_go,OUTPUT); // PIN 10 (PWM)
pinMode(Right_motor_back,OUTPUT); // PIN 11 (PWM)
pinMode(SensorRight_2, INPUT); // Middle Infrared as Input
}
How to get the input
We need to get the input by reading the state to a
variable.
SR_2 = digitalRead(SensorRight_2);
If (condition 1)
{
run if condition 1 is matched
}
else if (condition 2)
{
run if condition 2 is matched
}….
Making Decision
Say if we want the car to go forward if no obstacle, and if it see
obstacle , it will stop. You can write it like this
SR_2 = digitalRead(SensorRight_2);
If (SR_2 == HIGH) // no obstacle
{
forward();
}
else if (SR_2 == LOW) // there is obstacle
{
brake();
}
Making Decision
So, in the last example, the car will only stop when
there is an obstacle in front. Can you modify the
program such that it can avoid the obstacle and go
away from the obstacle ?
Reflective Photosensor
At the back of the Car, there are two pairs of
reflective photosensor to detect reflection from floor
Right Left
VR VR
Left Right
Sensor Sensor
at at
bottom bottom
Reflective Photosensor
With these two sensors, we can program the car to
follow a black line
Top
Bottom
Reflective Photosensor
If I want the car to follow the BLACK Line. How do
you write the program ?
Bluetooth
Nowadays, many application using Bluetooth to
communicate.
BT
Module
Control using BT
With the BT module and with the help of an App, we
can control our car using our Smart Phone
A Free BT Control App
Since the BT function is not Open for iOS devices,
we can only search for Free Apps on the Android
Devices
https://play.google.com/store/apps/details?id=com.inex.BlueStickControl
A Free BT Control App
BlueStick Control App
BlueStick will simulate your mobile phone as an
remote control.
C 8 D
4 0 6
E 2 F
void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
Serial.begin(9600);
digitalWrite(led, LOW);
void loop() {
if (Serial.available() > 0) {
char input = Serial.read();
if (input == ‘A’) {
digitalWrite(led, HIGH);
} else if (input == ‘B’) {
digitalWrite(led, LOW);
}
}
}
Modify your Sketch
The Bluetooth actually like the Serial function that
we described before.
So, in order to use the function, you need to
In the setup() part
Add Serial.begin(9600);
Modify your Sketch
In the loop() part
if (Serial.available() > 0) {
char input = Serial.read(); // Read the input
if (input == ‘A’) { // Check ‘A’
digitalWrite(led, HIGH); // perform action for A
} else if (input == ‘B’) { // Check ‘B’
digitalWrite(led, LOW); // perform action for B
} else if (input == ‘X’) { // more checkings
:
:
:
}
}
Modify your Sketch
So, start from your original program, can you modify
your program such that it can do the following
action using remote ?
Turn Turn
Stop
Left Right
Spin Backward
Spin
Left Right
Test
1. Turn on the BT
function on your
mobile phone
and scan for the
BT.
2. You should be
able to see a
device called
Formulate_Ex
3. Pair with it x can be
1 to 5
Test
1. Launch the
BlueStick App
2. Press Pair
3. Select the
Formulate_Ex
that you Paired x can be
before 1 to 5
4. Enjoy~
Tilt Mode
1. Press the Button
Mode button will
change to Tilt
Mode
2. See how does it
look like using
Tilt mode ?
Self reflection
Throughout the workshop, you have learned
• Basic Electronics
• Arduino
• Programming Structure of Arduino
• LED Experiment
• PWM
• Basic Programming Control statements.
• Using Computer to do Control
• Car Control using Sensor and Bluetooth
Summary
I hope after this workshop, you got experience on
how the electronics relate to Formulate E and when
Formulate E start, you can apply your knowledge to
it.
References : Electronics
• L. Richard Carley and Pradeep Khosla,
“Introduction to Electrical and Computer
Engineering- taught in Context”, The McGraw-Hill
Companies, Inc.
• G. Rizzoni, “Principles and Applications of
Electrical Engineering”, 5th edition, McGraw Hill,
2007
• D. V. Kerns and J.D. Irwin, “Essentials of
Electrical and Computer Engineering”, Pearson,
2004
• M. M. Mano, C.R. Kime, “Logic and Computer
Design fundamentals”, 3rd edition, Prentice-hall,
2004
References : Programming
in General
• Kjell Bäckman, “Structured Programming with
C++”
• Bruce Eckel, “Thinking in C++, Volume One:
Introduction to Standard C++”, 2nd Edition,
Prentice-Hall, 2000. ISBN: 0-13-979809-9.
• Bruce Eckel and Chuck Allison, “Thinking in C++,
Volume Two: Practical Programming”, Prentice-
Hall, 2004. ISBN: 0-13-035313-2.
References : Arduino and
Related Programming
• Arduino Official Website, https://www.arduino.cc/
• Arduino Official Tutorials,
https://www.arduino.cc/en/Tutorial/HomePage
• Control an Arduino with Bluetooth,
http://www.allaboutcircuits.com/projects/control-
an-arduino-using-your-phone/
• Building an Arduino Robot, Part I to Part VI,
http://blog.miguelgrinberg.com/post/building-an-
arduino-robot-part-i-hardware-components