arduino an introduction - peo scarborough · 2017-10-29 · arduino for mechatronics 2017 •this...

35
Arduino – An Introduction Hardware and Programming Presented by Madu Suthanan, P. Eng., FEC. Volunteer, Former Chair (2013-14) PEO Scarborough Chapter

Upload: others

Post on 17-Jun-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Arduino An Introduction - PEO Scarborough · 2017-10-29 · Arduino for Mechatronics 2017 •This note is for those contesting in the Mechatronics 2017 organized by PEOSC •You will

Arduino – An IntroductionHardware and ProgrammingPresented by

Madu Suthanan, P. Eng., FEC.

Volunteer, Former Chair (2013-14)

PEO Scarborough Chapter

Page 2: Arduino An Introduction - PEO Scarborough · 2017-10-29 · Arduino for Mechatronics 2017 •This note is for those contesting in the Mechatronics 2017 organized by PEOSC •You will

Arduino for Mechatronics 2017• This note is for those contesting in the Mechatronics 2017 organized by

PEOSC

• You will be shown the essential Arduino hardware, important components, and basic programming techniques for this

• I am a civil engineer whose hobbies include using Electronics, Arduino, Raspberry Pi, Beaglebone Black etc.

• After this presentation you should be able to incorporate and program Arduino for use in Mechatronics 2017, and other applications.

27 October, 2017

2

Page 3: Arduino An Introduction - PEO Scarborough · 2017-10-29 · Arduino for Mechatronics 2017 •This note is for those contesting in the Mechatronics 2017 organized by PEOSC •You will

Learning Outline• Section 1: Arduino Hardware▫ This will introduce you to the various important parts of Arduino.

• Section 2: Important components to use with Arduino: resistors, diodes, transistors, LEDs, motors and safety considerations▫ Voltage, and current requirements and limiting values

• Section 3: Arduino IDE (integrated Development Environment)▫ How to download the program from the Arduino.cc web-site

• Section 4: Arduino Programming (called sketches)▫ How to think in pseudo-code and write comments▫ Include Programming Codes (Sketches)▫ Run the sketches

• Section 5: Design your car to specifications▫ Face the judges▫ Race the cars – May the best win☺.

27 October, 2017

3

Page 4: Arduino An Introduction - PEO Scarborough · 2017-10-29 · Arduino for Mechatronics 2017 •This note is for those contesting in the Mechatronics 2017 organized by PEOSC •You will

Hardware are the physical parts that you can see, touch or kick

27 October, 2017

4

Page 5: Arduino An Introduction - PEO Scarborough · 2017-10-29 · Arduino for Mechatronics 2017 •This note is for those contesting in the Mechatronics 2017 organized by PEOSC •You will

S1. Objectives

At the end of this Section you will know

• the various important parts (and pins) of Arduino

• Safety and precautions to take when working with Arduino, electronics▫ About Limiting Voltages and Currents, to be covered in Section 2

• Breadboards (Prototyping Boards) and their use

• Printed Circuit Boards (PCB), soldering etc.

• How to power up Arduino.

27 October, 2017

5

Page 6: Arduino An Introduction - PEO Scarborough · 2017-10-29 · Arduino for Mechatronics 2017 •This note is for those contesting in the Mechatronics 2017 organized by PEOSC •You will

Section 1: About Arduino

• Arduino is an open source Microcontroller Development Board ▫ There are many models▫ Many equivalent compatible models are also available

• We use Arduino Uno Rev 3, the most popular model. ▫ It is built around Atmel’s ATmega328P microcontroller (μC)▫ It comes in 2 flavours:

Uses removable 28 DIL 328μC Uses 328μC surface mounted Device (SMD)

• We have supplied each type to your various teams.▫ For all your purposes they work the same way

27 October, 2017

6

Page 7: Arduino An Introduction - PEO Scarborough · 2017-10-29 · Arduino for Mechatronics 2017 •This note is for those contesting in the Mechatronics 2017 organized by PEOSC •You will

S1. The 2 models of Arduino UNO Rev 3 supplied28 pin DIL removable μC

328 μC

Surface Mounted Device (SMD) μC

328 μC

27 October, 2017

7

Page 8: Arduino An Introduction - PEO Scarborough · 2017-10-29 · Arduino for Mechatronics 2017 •This note is for those contesting in the Mechatronics 2017 organized by PEOSC •You will

The

imp

ort

ant

par

ts o

f A

RD

UIN

O U

NO

R3

The 2 rows of strips of Pins (actually sockets) are designed to accept sensors and actuators, and to piggyback accessory boards called Shields.5 Power Pins: Only Vin and Gnd is used to supply power to the board. You can use 6 to 20 V. Recommended is 7 to 12 V. You can also supply same value power through the Jack, but not Both.6 Analog IN pins (A0 to A5). To connect Analog sensors. These can measure 1024 levels of small voltages.14 digital IN/OUT pins (0 to 13). When set as INPUT, they can accept sensors. When set as OUTPUT, they can supply 5 (HIGH) or 0 (LOW) Volts to control Actuators. INPUT, OUTPUT, HIGH, LOW are specified in the Sketches.6 Analog OUT pins (3,5,6,9,10,11). These are actually Digital pins which can output PWM Pulse Width Modulated signals, which imitates analog output. They can be used to dim LEDs and change speeds of motors.

328μC

USB B

Power Jack

Voltage Regulator: 5V out

S127 October, 2017

8

Page 9: Arduino An Introduction - PEO Scarborough · 2017-10-29 · Arduino for Mechatronics 2017 •This note is for those contesting in the Mechatronics 2017 organized by PEOSC •You will

S1. Safety Considerations

• Electrically Arduino is very sensitive

• Supply voltages should not:▫ exceed 5 volts through the USB (normally USB voltages are regulated)

▫ Be within 6 to 20 Volts (recommended 7 to 12 volts), through the jack or Vin pin. A built-in Voltage regulator keeps the voltages supplied to the board at 5 volts.

• Output pins (0 to 13) supplies only 5 volts or 0 volts.

• Do not exceed current drawn from each pin 40 mA.

Pins are NOT overload or short-circuit protected. Easy to damage

27 October, 2017

9

Page 10: Arduino An Introduction - PEO Scarborough · 2017-10-29 · Arduino for Mechatronics 2017 •This note is for those contesting in the Mechatronics 2017 organized by PEOSC •You will

S1 – Breadboards and their use

• More aptly called Prototyping Boards

▫ Used mainly to try out circuits easily as component can be plugged in wires connected and retried easily.

▫ Once finalised, final circuit boards are made and the components are soldered in.

• Please note:

▫ a1 to e1 (5 holes) – Row 1 Left bank (L): are internally connected.

▫ The internal connection for Row 3 L is shown.

▫ similarly all rows 1L to 30L, and rows 1R to 30R.

▫ The + holes are all internally connected (as shown)

▫ Similarly the – holes on Left, and + and – holes on right.

• This provides an easy way of wiring your components and trying your circuits for proper working, and measure voltages, currents, resistances etc.

27 October, 2017

10

Page 11: Arduino An Introduction - PEO Scarborough · 2017-10-29 · Arduino for Mechatronics 2017 •This note is for those contesting in the Mechatronics 2017 organized by PEOSC •You will

S1 – Printed Circuit Boards (PCB)• Once your prototypes are working, you can transfer your circuit to a

Printed Circuit Board, if highly reliable circuits are desired.

• Printed Circuit Boards can be customised to your needs.

▫ Useful if you are manufacturing many similar circuits.

• You can also buy circuit boards which imitate the Breadboard layout.

• What we have supplied is a simple one without any traces, but can be soldered the way you wish.

▫ It is a Universal Printed Circuit Board.

• Soldering is a good skill to master.

• Please do it under the supervision of an experienced teacher, parent or other adult.

27 October, 2017

11

Page 12: Arduino An Introduction - PEO Scarborough · 2017-10-29 · Arduino for Mechatronics 2017 •This note is for those contesting in the Mechatronics 2017 organized by PEOSC •You will

S 1: Wrap-up

• Use your Arduino carefully

• It can be easily damaged by over voltage and overcurrent

• Once you use it carefully, it is very rugged and has long life

• Apply only recommended voltage to the specified points

• Any Questions before we go to the next section?

27 October, 2017

12

Page 13: Arduino An Introduction - PEO Scarborough · 2017-10-29 · Arduino for Mechatronics 2017 •This note is for those contesting in the Mechatronics 2017 organized by PEOSC •You will

Arduino uses Input sensors and output Actuators.

Sensors: Switches, Light, Sound, Pressure, Temperature, Shock, Proximity, Position and others

Actuators: LED, Motors, Speakers, Lasers and others

Other components required: resistors, diodes, transistors, capacitors, inductors etc.

27 October, 2017

13

Page 14: Arduino An Introduction - PEO Scarborough · 2017-10-29 · Arduino for Mechatronics 2017 •This note is for those contesting in the Mechatronics 2017 organized by PEOSC •You will

S2- Objectives

At the end of this Section you will know

• the various important components used with Arduino▫ Understanding their values, ratings, and intended uses.

• Safety and precautions to take with components in electronics and Arduino▫ About Limiting Voltages and Currents, Wattages

Well selected components with make the project work reliably as intended and lasts very long – years for most cases.

27 October, 2017

14

Page 15: Arduino An Introduction - PEO Scarborough · 2017-10-29 · Arduino for Mechatronics 2017 •This note is for those contesting in the Mechatronics 2017 organized by PEOSC •You will

Main components used in Mechatronics 2017

• Resistors

• Darlington Transistor

• LEDs

• Motors

Read about above components in Part 1 of the Guidelines.

Read about other components supplied and their uses for designing more advanced projects (after the contest)

We have supplied LDR, diodes, transistors, capacitors etc. for your learning and use at your own pace after the contest.

27 October, 2017

15

Page 16: Arduino An Introduction - PEO Scarborough · 2017-10-29 · Arduino for Mechatronics 2017 •This note is for those contesting in the Mechatronics 2017 organized by PEOSC •You will

Precautions when using components

• Each component has:▫ Name & Value.

Learn to recognise a component and read its value and the rated maximum voltage, current and power (Watt).

Use on-line Datasheets to get full details of components

Values are sometimes colour codes (resistances)

Wattage is sometimes by size of component

Sometimes resistors and inductors look same (we have not supplied inductors)

• Part 1: Guidelines and notes in our web-site give good information on these.

27 October, 2017

16

Page 17: Arduino An Introduction - PEO Scarborough · 2017-10-29 · Arduino for Mechatronics 2017 •This note is for those contesting in the Mechatronics 2017 organized by PEOSC •You will

Connecting LEDs

• Can I connect an LED directly to an Arduino OUTPUT pin and ground?

• NO!

• Why not?

• Because the LED may burn out and the Arduino may get damaged.

• Explain.

▫ LEDs drop about 1.7 volts to 2.4 volts depending on the colour and size.

▫ Output pins of Arduino supply 5 Volts.

▫ So if an LED is connected directly, it will overload the Arduino and the LED!

• So how do we connect an LED to Arduino?

• Use a suitable to resistance in series. Th resistance has to drop 5 – 2 = 3 volts.

• Also the current should not exceed 40 mA (for the Arduino or 20 mA for the LED. Try to keep it to say 10 mA. = .01 Amp.

• What is the resistance required?

• By Ohms Law:

R=V/I = 3/.01 =300 ohms.

We can use the nearest available resistor. If necessary you can connect resistors in series or parallel to get a suitable value.

• What should be the power rating of the resistor? = I2R = .012x300 = 0.03 W. So use ¼ W.

27 October, 2017

17

Page 18: Arduino An Introduction - PEO Scarborough · 2017-10-29 · Arduino for Mechatronics 2017 •This note is for those contesting in the Mechatronics 2017 organized by PEOSC •You will

Example of connecting LEDs

• Diagram shows how an LED is connected to Ground and pin 10

• Note to connect the LED the right way round.

• It lights only if the Anode (the longer leg) is connected to positive side.

• The resistor is connected in series.

• You can connect more than one LED and in the program you can make them blink in various orders. (Not required for Juniors for Mechatronics 2017)

27 October, 2017

18

Page 19: Arduino An Introduction - PEO Scarborough · 2017-10-29 · Arduino for Mechatronics 2017 •This note is for those contesting in the Mechatronics 2017 organized by PEOSC •You will

Connecting motors to Arduino

• Can I connect a 6 V DC motor directly to an OUTPUT pin of the Arduino?

• Oh NO!

• Why not?

• Arduino will be fried!

• A motor when running may take 100 mA or more. When stalled (shaft stopped) it may draw more than 1 Amp. (Check these by using a meter carefully set to Amp).

• Arduino can supply only 40 mA.

• Solution. Use a Transistor which can take this current safely. Read about Darlington Transistor (TIP120) and how to connect them and use of a resistor in our Guidelines and Notes Part 1.

Next slide I give a possible circuit. Use your own ingenuity and creativity to improve on it.

27 October, 2017

19

Page 20: Arduino An Introduction - PEO Scarborough · 2017-10-29 · Arduino for Mechatronics 2017 •This note is for those contesting in the Mechatronics 2017 organized by PEOSC •You will

I have given a possible sample circuit for the car project for Mechatronics 2017.

The software to make the car run is given in next Sections.

How the car is to be started or stopped etc. is in our web-site or given during the contest.

I have not shown any switch or sensors. So remove the battery wire when not in use.A resistor is used in the base circuit to limit the current through the base.

27 October, 2017

20

Page 21: Arduino An Introduction - PEO Scarborough · 2017-10-29 · Arduino for Mechatronics 2017 •This note is for those contesting in the Mechatronics 2017 organized by PEOSC •You will

The freely downloadable Arduino IDE is used to program the Arduino. Arduino programs are called Sketches in Arduino parlance.

You can use the sketches on line or download and .exe executable program or a Zip file to be unzipped. Get help from a teacher or adult if you have problems.

27 October, 2017

21

Page 22: Arduino An Introduction - PEO Scarborough · 2017-10-29 · Arduino for Mechatronics 2017 •This note is for those contesting in the Mechatronics 2017 organized by PEOSC •You will

S3 - Objectives

At the end of this Section you will be able to

• Download the Arduino IDE from the Arduino web-site▫ You can also use the program on-line and save your files on the Cloud

Note: Some of the schools and offices may have protected the computers from downloading programs▫ If so, you have to inform the IT Department and get this site downloadable

and accessible.

27 October, 2017

22

Page 23: Arduino An Introduction - PEO Scarborough · 2017-10-29 · Arduino for Mechatronics 2017 •This note is for those contesting in the Mechatronics 2017 organized by PEOSC •You will

Installing the Software IDE

• You can install the IDE into Windows, MacOS or Linus Computers.

• Please visit: http://Arduino.cc for the IDE.

• More specifically, you can download from:

http://Arduino.cc/en/Main/Software

• Detailed installation instructions are available at: http://Arduino.cc/en/Guide/HomePage

• Connect the supplied Arduino to your computer using the supplied USB A/B cable or a USB printer cable.

• Make sure the correct COM port is selected to connect to the Arduino and select UNO model.

27 October, 2017

23

Page 24: Arduino An Introduction - PEO Scarborough · 2017-10-29 · Arduino for Mechatronics 2017 •This note is for those contesting in the Mechatronics 2017 organized by PEOSC •You will

Examples• There are a number of example sketches

(programs) under: File>Examples.• Blink is a nice sketch usually pre-loaded into new

Arduinos.File>Examples>01. Basics>Blink.

• It makes the built-in LED at pin13 blink.• See how you can modify the sketch to make it

blink slower or faster.• See how you can connect an external LED to

make it Blink at various speeds, or blink solidly.• If error messages appear while verifying or

uploading, make sure that the correct COM port is selected, and there are no errors in the sketches. When an error occurs usually the cursor stops against the line where the error occurred. Many of the error messages are misleading and not clear enough.

• In the next section we look at the elements of programming in more detail.

27 October, 2017

24

Page 25: Arduino An Introduction - PEO Scarborough · 2017-10-29 · Arduino for Mechatronics 2017 •This note is for those contesting in the Mechatronics 2017 organized by PEOSC •You will

Arduino uses C Programming language modified especially for use with Arduino. It is called Arduino IDE and is freely downloadable from Arduino.cc or you can work on line in their site.

There are many examples and projects at this site for you to learn from.

27 October, 2017

25

Page 26: Arduino An Introduction - PEO Scarborough · 2017-10-29 · Arduino for Mechatronics 2017 •This note is for those contesting in the Mechatronics 2017 organized by PEOSC •You will

Section 4 - Objectives

At the end of this Section you will learn• Some programming techniques which are common to all programs▫ the format of the sketches

• Use pseudo-Code – think in your natural language▫ Use comments to make your sketches easier to understand

• a few Arduino Commands▫ to write a working sketch▫ To Verify/Compile Sketch▫ To Upload to Arduino and run the sketch

• The use of the Serial Monitor

27 October, 2017

26

Page 27: Arduino An Introduction - PEO Scarborough · 2017-10-29 · Arduino for Mechatronics 2017 •This note is for those contesting in the Mechatronics 2017 organized by PEOSC •You will

Programming Techniques

• Every programming language follow certain methods.• Programming languages are different to natural languages as they are

very precise and does not lend to double meanings.• Each language has a set of commands or keywords which has to be

written in its proper syntax.• If programming looks very daunting a first, relax. You can think in your

own natural language and write down what the program has to do.▫ This is called pseudo-coding

• Some prefer flowcharting and other methods, but pseudo-coding is easy. It can be incorporated inside the sketch itself as comments.

27 October, 2017

27

Page 28: Arduino An Introduction - PEO Scarborough · 2017-10-29 · Arduino for Mechatronics 2017 •This note is for those contesting in the Mechatronics 2017 organized by PEOSC •You will

Format of sketches

• Arduino Sketches consists of 2 functions called:

▫ void setup () { }

▫ void loop () { }

▫ If a function runs into many lines, each line has to end with a ; (semi colon)

▫ You can also add comments to make the sketch understandable to a reader.

▫ The functions will use commands with arguments which has to follow some simple rules. They are case sensitive.

Comments are text entered starting with

/* and ending with */

This can be multiline.

Or comments can be single line starting anywhere on a line with //.

Comments are useful for you to read and understand the sketches. They are not read by the compiler of the IDE.

Commands are single words in the format:

pinMode, digitalWrite, etc.

They are case sensitive: Pinmode or PinModepin Mode will no be accepted.

27 October, 2017

28

Page 29: Arduino An Introduction - PEO Scarborough · 2017-10-29 · Arduino for Mechatronics 2017 •This note is for those contesting in the Mechatronics 2017 organized by PEOSC •You will

Format of sketches contd..

• Pins have to be defined as INPUT or OUTPUT which are predefined constants in Arduino

• You can also define variable such as ledPin, which would mean that pin is connected to an LED.

• Also you can define variables as int meaning Integer or full number between -32,768 to +32,767.

• You can get a full list from Arduino site or the books referred to in our rules.

• Let me explain further with an example:

27 October, 2017

29

Page 30: Arduino An Introduction - PEO Scarborough · 2017-10-29 · Arduino for Mechatronics 2017 •This note is for those contesting in the Mechatronics 2017 organized by PEOSC •You will

Example sketch – to blink an LED/* This is an example sketch to blink an LED. We can modify this to blink more LEDs and to run motors etc.*/// Blink by Madu – 27 Oct 2017int ledPin = 8; // LED is connected to pin 8void setup(){pinMode (ledPin, OUTPUT); // Pin 8 is set for output}void loop(){digital Write (ledPin, HIGH); // set LED ondelay (1000); //wait 1 sec. 1000 is 1000 msec.digitalWrite (ledPin, LOW); //make LED offdelay (2000); // wait 2 seconds}

• See how we have commented so that the lines of codes are clearly understood by a reader.

• void setup () runs only once through the sketch• void loop () runs endlessly. When it comes to the

end of the sketch it run through the loop again and again.

• It is possible to stop it going round in loops by more advanced commands, not covered here.

• With slight modifications you can make the LEDs blink at different rates. Also you can connect LEDs to different pins.

• You can also make the LEDs work like turn signals, hazard signals in a car or as traffic light and more!

• You can also make the LEDs on without blinking.

27 October, 2017

30

Page 31: Arduino An Introduction - PEO Scarborough · 2017-10-29 · Arduino for Mechatronics 2017 •This note is for those contesting in the Mechatronics 2017 organized by PEOSC •You will

Example sketch - To run a motor• How will you make the LEDs glow without

blinking?• How can you replace an LED with a motor (with a

proper transistor to allow the high current?• How can you add blinking LEDs in addition to the

motor?• How can you make the LEDs dimmer? Or the

motor run at different speeds?▫ This is where you use PWM. Pulse Width

Modulation. Pins marked with ~ are capable of pulse Width Modulation. It is not necessary for the motor as you want to run the car as fast as possible.

▫ Seniors can try to dim their LEDs if they so desire. I am not covering PWM and its commands here. Students can do their own reading/research.

/* This is an example sketch to run a motor. We can modify this to include blinking of many LEDs etc.*/// Run motor by Madu – 27 Oct 2017int motorPin = 9; // Motor is connected to pin 9void setup(){pinMode (motorPin, OUTPUT); // Pin 9 is set for outputdigitalWrite (motorPin, HIGH); set PIN 9 as high}void loop(){}

27 October, 2017

31

Page 32: Arduino An Introduction - PEO Scarborough · 2017-10-29 · Arduino for Mechatronics 2017 •This note is for those contesting in the Mechatronics 2017 organized by PEOSC •You will

Read the specifications carefully. Follow the Notes, Questions and Answers and the exercises given. They help to make a better design, face the Judges and race your car.

27 October, 2017

32

Page 33: Arduino An Introduction - PEO Scarborough · 2017-10-29 · Arduino for Mechatronics 2017 •This note is for those contesting in the Mechatronics 2017 organized by PEOSC •You will

Section 5: Objectives

At the end of this Section you will be able to

• Design and fabricate a good working car▫ Program it to work as designed

• Face the Judges and their questions▫ Get good confidence and self-esteem about facing interviews

• Race your car.

• Winning is not an objective

• Participation and comradeship is the main objective

▫ Winning is no objection☺. May all be winners.

27 October, 2017

33

Page 34: Arduino An Introduction - PEO Scarborough · 2017-10-29 · Arduino for Mechatronics 2017 •This note is for those contesting in the Mechatronics 2017 organized by PEOSC •You will

Please read the rules and specifications for the car

• Read the rules for the contest, specifications and the Guidelines, Notes and Questions and Answers given in our website.

• You can use parts from old toy cars and use the wheels, frame etc. from them. Try not to use a drivetrain from such a car as you may lose marks.

• Work as a team and share your knowledge with other teams. Winning should not be the main objective.

• Teamwork, comradeship and good presentation skills to the judges are important.

• Your knowledge can be shown to the judges even if your car has not come out as expected. Prepare good notes and diagrams, even by hand. Computer prepared notes, and diagrams do not carry extra marks.

27 October, 2017

34

Page 35: Arduino An Introduction - PEO Scarborough · 2017-10-29 · Arduino for Mechatronics 2017 •This note is for those contesting in the Mechatronics 2017 organized by PEOSC •You will

Thank you and good luck to all teams

27 October, 2017

35