Sei sulla pagina 1di 100

Understanding Formulae E

Workshop for the Talented


In the formulae E, the cars are controlled by human
with the help of electronics.
In this section, we will try to introduce you how
electronics can control the cars and how you can use
simple program to do the control.
Understanding Formulae E
Workshop for the Talented
• Basic Electronics
• Overview of Arduino
• IDE environment
• Programming Structure of Arduino
• LED Experiment, Exercise
• PWM
• Basic Programming Control statements.
• Using Computer to do Control
• Exercise
• Car Control
Basic Electronics
In the electronics world, there are some basic things
you need to know.
1. Conductor and Insulators
2. Resistor
3. Voltage
4. Current
Conductors and Insulators
Conductors refer to the materials that allows
electrons to flow.
Examples : Metals, Copper Wires

Insulators refer to the materials that does not allow


electrons to flow.
Examples : Plastics, Wood
Resistance and Resistor
Resistance is a term to describe how easy the
electrons to flow
Metals, Copper Wires – Low resistance
Plastics, Wood – High resistance

Later on we will use Resistor – a material with fixed


resistance in our workshop.
Voltage
In simple sense, Voltage is the difference in electric
potential energy between two points.

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. */
}

You need to include both functions in your sketch, even


if you don't need them for anything.
Your First Project
• In this program, we will try to understand the
basic output of Arduino
• We will try to turn on and off and LED on the
Arduino
Your First Program
• There is an LED which is connected to Pin 13 for
testing purpose.
LED sketch
/* Turns on an LED on for one second, then off for one second, repeatedly.

This example code is in the public domain. */

// Pin 13 has an LED connected on most Arduino boards.

int led = 13;

// the setup routine runs once when you press reset:

void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);

// the loop routine runs over and over again forever:

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

You should see


the board blinking
after the program
is successfully
uploaded 
LED sketch
/* Turns on an LED on for one second, then off for one second, repeatedly.

This example code is in the public domain. */

// Pin 13 has an LED connected on most Arduino boards.

int led = 13;

// the setup routine runs once when you press reset:


What is the use of this
void setup() {
statement ?
// initialize the digital pin as an output.
pinMode(led, OUTPUT);

// the loop routine runs over and over again forever:

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

Pin 13 HIGH LOW HIGH LOW HIGH LOW


Status

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.

This example code is in the public domain. */

// Pin 13 has an LED connected on most Arduino boards.

int led = 13;

// the setup routine runs once when you press reset:

void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);

// the loop routine runs over and over again forever:

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

} What will you expect to see ? 


Timing
If we use back the timing diagram, which part
changed ?

Pin 13 HIGH LOW HIGH LOW HIGH LOW


Status

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 ?

Pin 13 HIGH LOW HIGH LOW HIGH LOW


Status

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

Pin 13 HIGH LOW HIGH LOW HIGH LOW


Status
50% ON

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

Pin 13 HIGH LOW HIGH LOW HIGH LOW


Status

time
0.001 0.001 0.001 0.001 0.001 0.001
second second second second second second

HIGH LOW HIGH


TIME TIME TIME

PERIOD
Period and Frequency
From the previous figure
HIGHTIME + LOWTIME = PERIOD
PERIOD means time for one cycle

Frequency =
௉ாோூை஽

Frequency is how fast the signal changing.


Pulse Width Modulation
The theory behind in the past example is called
Pulse Width Modulation.
A PWM signal is a fast switching ON/OFF signal
that to simulate an Analog signal.
Next page show some examples
Pulse Width Modulation
PWM in Arduino
Arduino have some pins that allow you to output
PWM signals
~ 11
~ 10
~9
~6
~5
~3
Change your program
int led = 11;

// the setup routine runs once when you press reset:

void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
}

// the loop routine runs over and over again forever:

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
}

There is a common variation called if-else that


looks like this:
if (someCondition) {
// do stuff if the condition is true
} else {
// do stuff if the condition is false
}
If statement
There's also the else-if, where you can check a
second condition if the first is false:
if (someCondition) {
// do stuff if the condition is true
} else if (anotherCondition) {
// do stuff only if the first condition is false
// and the second condition is true
}
For statement
The for statement is used to repeat a block of
statements enclosed in curly braces. An increment
counter is usually used to increment and terminate
the loop.
The for statement is useful for any repetitive
operation, and is often used in combination with
arrays to operate on collections of data/pins.
for statements
There are three parts to the for loop header:
for (initialization; condition; increment) {
//statement(s);
}
for statements

The initialization happens first and exactly once.


Each time through the loop, the condition is tested; if
it's true, the statement block, and the increment is
executed, then the condition is tested again. When
the condition becomes false, the loop ends.
Control by Computer
In the previous LED examples, when you finish the
program, and upload the program to the board, it
will run according to what you wrote in the loop, and
run continuously.
After you modify the program, you need to recompile
it and upload it again.
Since the Arduino is connected to the PC now,
actually you can control the board.
Let’s go back to your first LED Sketch
Use Computer to Control
LED
int led = 13;
// the setup routine runs once when you press reset:

void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
Serial.begin(9600);
digitalWrite(led, LOW);

// the loop routine runs over and over again forever:

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

This is the control from


computer, try to type A
then click send and see
what will happen ?
How about B ?
From LED to Motor
What is a motor ?
Motor will change
The electrical energy
into movement.
Motor Movement
How to make a motor move ?
Normally a motor will have 2 terminals. Pictorially
will be represented as

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

Left_motor_go Left_motor_back Right_motor_go Right_motor_back


=9 =8 = 10 = 11

Forward

If I want the car to go Forward, what should be


applied to the pin (HIGH/LOW) ?
How about Stop ?
Motor sketch
// Car Go Forwarding Experiment
int Left_motor_go=9; // Left Motor Forward (IN2)
int Left_motor_back=8; // Left Motor Backward (IN1)
int Right_motor_go=10; // Right Motor Forward (IN3)
int Right_motor_back=11; // Right Motor Backward (IN4)

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_motor_go Left_motor_back Right_motor_go Right_motor_back


=9 =8 = 10 = 11

Left
Turn
Right
Turn
Motor Control Turn
Can we make Turn Left and turn Right faster ? (i.e.
Spin ? )

Left_motor_go Left_motor_back Right_motor_go Right_motor_back


=9 =8 = 10 = 11

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.

Infra Red Infra Red


Transmitte Receiver
r
Infra Red Sensor
The infra red sensor is connected to the Pin 5 of the
Arduino
Infra Red sketch
// Car Go Forwarding Experiment
int Left_motor_go=9; // Left Motor Forward (IN2)
int Left_motor_back=8; // Left Motor Backward (IN1)

int Right_motor_go=10; // Right Motor Forward (IN3)


int Right_motor_back=11; // Right Motor Backward (IN4)

const int SensorRight_2 = 5; // Middle Infra Red Sensor (P3.4 OUT3)

int SR_2; // Sensor Status

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 there is obstacle SR_2 will be LOW


If there is No obstacle SR_2 will be HIGH
Making Decision
The car should do different things based on different
status. It can be done by if statement

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

Right Right Left Left


Tx Rx Tx Rx
Reflective Photosensor
The reflective photosensor can sense the reflection
from the floor. If the floor is Black, it will not be able
to reflect the light.
Reflective Photosensor
In our car, the photosensors are connected to
SensorRight = 3; // Right Bottom Photo Sensor
SensorLeft = 4; // Left Bottom Photo Sensor
We also defined the status
int SL; // Left Sensor Status
int SR; // Right Sensor Status
Reflective Photosensor
The return status of the photosensor is as follow
If the floor is BLACK, it will output HIGH
If the floor is WHITE, it will output LOW
Reflective Photosensor
Left LED Right
indicate LED
have indicate
reflection have
on Left reflection
Sensor on Right
Sensor

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.

Bluetooth is a wireless technology standard for


exchanging data over short distances from fixed and
mobile devices, and building personal area networks
(PANs).
Bluetooth module on our
Car
At the top of our car, there is a Bluetooth module
connected to it.

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

In the Google Play Store, search for an App called


“BlueStick”

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.

The App has two mode


1. Button Mode
2. Tilt Mode
We will concentrate on the Button Mode first
BlueStick Button Mode

C 8 D

4 0 6

E 2 F

There are 9 buttons on the App


for control
Each button is mapped with one
character as shown above
Modify your Sketch
Remember before how we can control the LEDs via
the computer ?
Use Computer to Control
LED
int led = 13;
// the setup routine runs once when you press reset:

void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
Serial.begin(9600);
digitalWrite(led, LOW);

// the loop routine runs over and over again forever:

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 ?

N/A Forward N/A

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

Potrebbero piacerti anche