ME
4215Q
Kỹ thuật
công nghệ
ME4215Q
KỸ THUẬT CÔNG NGHỆ
Lecture 3a, Robotics/Mechatronics:
• Arduino programming to control motors and sensors
But first….
ME
4215Q
Kỹ thuật
công nghệ
• We will use a tool to help us to review teamwork in the
RGB project. Please fill out the questionnaire emailed to
you overnight.
ME
Objectives
4215Q
Kỹ thuật
công nghệ
The objectives of this lecture are:
• to identify the functions of a Microcontroller
• give the basics of programming an Arduino
• show how to make simple connections to hardware
You should review this lecture with lecture 3b –
Mechatronic elements which gives more detail of the
hardware you’ll attach to the Arduino
Microcontroller
Sensor Motor
ME
Introduction
4215Q
Kỹ thuật
công nghệ
https://youtu.be/JlRPICfnmhw
ME
Introduction
4215Q
Kỹ thuật
công nghệ
Microcontroller
Vision sensor
Sound sensor
Motors
Power supply
Inertial sensor
Tilt sensor
Tactile sensor
Force sensor
Pressure sensor
ME
Applications
4215Q
Kỹ thuật
công nghệ
Consumer, medical, amusement
Sensors,
Motors, Automobiles
Microcontroller
Communications
Aerospace & security
Micro/Nano Technology
Manufacturing, FA
ME
Microcontroller Examples
4215Q
Kỹ thuật
công nghệ
• PIC Microchip
• 68HC11 of Motorola
• 8096 of Intel
• Arduino
• Raspberry Pi
•…
Different models of ‘arduino’
ME
4215Q
Kỹ thuật
công nghệ
https://www.arduino.cc/en/Main/Products
ME
Control diagram
4215Q
Kỹ thuật
công nghệ
Sensor Input: Motor Output:
Mechanical, thermal, Microcontroller Mechanical effort, motion,
electromagnetic, optical, torque
sound,etc.
Control
Sensors Motors
Software
This lecture (part a) focuses on the microcontroller and software.
Part b focuses on sensors and motors (and how to connect them).
Program (‘sketch’) structure
ME
4215Q
Kỹ thuật
công nghệ
setup
• runs once
loop
• runs from top to
bottom - for ever -
until powered off
Parts of the ‘language’
ME
4215Q
Kỹ thuật
công nghệ
‘reserve’ words
show in a colour
Variables store
value – e.g. a
number or
Functions carry
out actions
Structure lets us
repeat operations
A ‘sketch’
ME
4215Q
Kỹ thuật
công nghệ
uses a defined structure
uses the ‘language’ with
variables, functions and
structures
// has comments (grey
words)//
/*& multi line comments
including blank lines*/
ME
Get started
4215Q
Kỹ thuật
công nghệ
Please go to www.arduino.cc
Here you can:
• Get help
• Download software
• See examples
ME
Online or download?
4215Q
Kỹ thuật
công nghệ
• downloaded • online
ME
Is it working?
4215Q
Kỹ thuật
công nghệ
Is the board connected?
Look in tools – can you
‘Get board info’?
>File > Examples > 0.1
Basics > Blink >
upload the sketch
Does the built-in LED
flash continually?
ME
Basic structure
4215Q
Kỹ thuật
công nghệ
setup runs only once – at the
beginning. We use it to set
things up and define
variables, etc.
loop runs forever – it’s hard to
stop. We use it to carry out
instructions repeatedly.
20
ME
Example sketch for LDR
4215Q
Kỹ thuật
công nghệ
int analog; // declare an integer variable first
void setup() This sketch
{ changes the blink
Serial.begin(9600); // enable serial communication rate of the LED
pinMode(8,OUTPUT); // define LED pin 8 as output based upon the
pinMode(9,OUTPUT); // define LED pin 9 as output brightness of the
light dependent
digitalWrite(8,LOW); // set LED pin 8 as GND (0 V)
resistor (LDR).
} An LDR looks
like this!
void loop()
{
analog = analogRead(0); // read voltage on pin 0 and store it in variable analog
Serial.println(analog); // display the value of analog to the monitor
digitalWrite(9,LOW); // turn on the LED
delay(analog); // delay based on the analog reading
digitalWrite(9,LOW); // turn on the LED
delay(analog); // delay based on the analog reading
}
ME
Your turn
4215Q
Kỹ thuật
công nghệ
• Please work through
worksheet 0.
• Feel free to help
each other and to
ask for help from
me.
ME
Variables
4215Q
Kỹ thuật
công nghệ
Variables must be defined before they are used.
Basic types:
• byte = 8 bit unsigned number (0 to 255)
• int = (integer) 16 bit number (n -32768 to +32767)
• unsigned int = positive integers only (0 to 65535)
• A float = 32 bit floating-point numbers (3.4028235E+38 to -3.4028235E+38); numbers
that have a decimal point with decimal precision of up to 6 digits
Example of proper definitions:
• byte mark; \\ this variable is called mark, and can have any value between 0 and 255
• int variable_1; \\ variable_1 is an integer - it have any value between -32768 and +32767
• unsigned int y = 5; \\ y can be anything between 0 and 65535, this value is initiated as 5
• float not_pi = 3.14155; \\ not_pi is a floating-point value initiated as 3.14155
• More variable types here: https://www.arduino.cc/en/Reference/HomePage
ME
Functions
4215Q
Kỹ thuật
công nghệ
Functions are used to control the Arduino project board and for carrying out
computations.
Physical input and output - digital:
• pinMode() set the pin, numbered in the bracket,
to be INPUT or OUTPUT (or INPUT_PULLUP)
• digitalWrite() write HIGH or LOW to the pin numbered in the bracket
• digitalRead() read HIGH or LOW from the pin numbered in the bracket
Physical input and output - analogue:
• analogRead() read a value from the pin numbered in the bracket with up to 10
bit precision – that’s 210 or 0-1023.
• analogWrite() write a value to the pin numbered in the bracket. The value is
written as a PWM wave between 0 and 256
pulse width
ME
More Functions
4215Q
Kỹ thuật
công nghệ
Other functions work with:
Time:
delay(), delayMicroseconds() micros(), millis() put the value wanted in
the brackets)
Maths:
abs(), max(), min(), sq(), sqrt() etc. for calculations
Trigonometry:
cos(), sin(), tan() for trig calculations
Many others: random numbers, character manipulations, communications, bit &
byte manipulations, etc. See: https://www.arduino.cc/reference/en/#functions
ME
Control structures
4215Q
Kỹ thuật
công nghệ
• Loop keeps on doing operations until the Arduino is
stopped – great if I, say, want to continually monitor
temperature.
• What if I want to do an operation just 10 times – e.g. turn
a motor through 10 revolutions – and then go on to do
something else?
• We use control structures for this (see Arduino Reference)
• The main control structures are:
• if (if some condition is met, do this)
• else if (else if a different condition is met, do something else)
• for (typically, for a certain number of times, do this)
• while (while the condition inside the brackets is true, keep
doing this)
An ‘if’ example
ME
4215Q
Kỹ thuật
công nghệ
byte x = 0; // declare variable x outside of
// void – to be used everywhere
void setup (){
pinMode(13, OUTPUT); // enable pin 13 as digital output
}
void loop (){
delay(1000); // delay for 1 second
if(x == 5){ // if x is equal to 5, do the
// following steps
digitalWrite(13, HIGH); // set pin 13 to HIGH
delay(100); // delay for 100 ms
digitalWrite(13, LOW); // set pin 13 to LOW
x = 0; // reset x to 0
}
x++; // increases x by one (this is shorthand for x = x + 1)
}
ME
if, else if, else
4215Q
Kỹ thuật
công nghệ
• If () and else if() have conditionals, however else does not.
• Structure of if/else:
if(// first condition is true // ){
// do code between these curly brackets only
}
else if(// second condition is true //){
// do code between these curly brackets only
}
else{ // neither first or second condition were true
// do code between these curly brackets only
}
// end of if statement
• Note: as many else if statements can be included as necessary. If no else
statement is included, the code will continue from the next line after the if
statement if the conditional is false.
ME
while
4215Q
Kỹ thuật
công nghệ
int i = 0; // initiate i as an int that is equal to 0;
while(i <= 10) { // while (condition){} code is repeated
// until condition is no longer true
i++ ; // increment (i++) must be performed
//inside the loop
}
• What will happen if i++ is not included somewhere inside
the loop?
• Always modify the conditional variable inside the loop or
the condition will never become false and the loop will
never end
Structure of a ‘for’ loop
ME
4215Q
Kỹ thuật
công nghệ
• for and while loops have slightly different structures or
‘syntax’. It is important to take note of these differences,
as using the incorrect structure will result in errors.
for(i = 0; i <= 10; i++) {
do what’s inside the curly brackets
} until the condition is no longer true
initiation
condition
increment
• What will the value of i be for each loop?
ME
Comparison & Boolean Operators
4215Q
Kỹ thuật
công nghệ
The result of a conditional is either true (1) or false (0)
any integer value over 1 is true, e.g. 2 is true
• == equal to e.g. (2 == 2) true
• != not equal to e.g. (3 != 1) true
• < less than e.g. (1 < 1) false
• > greater than
• <= less than or equal to
• >= greater than or equal to
• && and e.g. (2 == 2) && (1 < 1) false
• || or e.g. (2 == 2) || (1 < 1) true
• ! not e.g. !(2 == 2) false
ME
Connecting the pins
4215Q
Kỹ thuật
công nghệ
Our software (‘sketches’) communicate with hardware
(motors and sensors) through pins.
Examples: if a small motor is connected to
pins D3 and D13 to which PWM
if a LED is connected to digital
is written, the motor will spin
pin 13, and pin 13 goes HIGH,
then the LED will illuminate.
ME
Worksheets
4215Q
Kỹ thuật
công nghệ
ME
Do the Worksheets
4215Q
Kỹ thuật
công nghệ
• This lecture only intended 0. getting started
to give an overview. 1. LEDs & Blink
• Find the worksheets in 2. variables & control
‘Toolbox’. They will give structures
you time to work through 3. digital inputs
examples.
4. analog inputs
5. analog input and output
6. subroutines & conditions
7. motors
8. servo motors
9. stepper motors