45 PROJECTS FOR BEGINNERS AND EXPERTS Bert van Dam
ISBN 978-1-907920-49-3
You can use it as a projects book and build more than 45 projects for your own use. The clear explanations, schematics, and pictures of each project make this a fun activity. The pictures are taken of a working project, so you know for sure that they are correct. You can combine the projects in this book to make your own projects. To facilitate this, clear explanations are provided on how the project works and why it has been designed the way it has. That way you will learn a lot about the project and the parts used, knowledge that you can use in your own projects.
45 PROJECTS FOR BEGINNERS AND EXPERTS
ARDUINO UNO
● BERT VAN DAM
Bert van Dam is a freelance author of project books, courseware and articles on microcontrollers including ARM and PIC devices. In his publications he also covers Raspberry Pi, artificial intelligence and several programming languages like JAL, C, assembler, Python, and Flowcode.
This book covers a series of exciting and fun projects for the Arduino, such as a silent alarm, people sensor, light sensor, motor control and an internet and wireless control (using a radio link). Contrary to many free projects on the internet, all projects in this book have been extensively tested and are guaranteed to work!
ARDUINO UNO – 45 PROJECTS
ARDUINO UNO
Apart from that, the book can be used as a reference guide. Using the index, you can easily locate projects that serve as examples for the C++ commands and Arduino functionality. Even after you’ve built all the projects in this book, it will still be a valuable reference guide to keep next to your PC. LEARN
www.elektor.com
DESIGN
Elektor International Media BV
Bert van Dam LEARN
DESIGN
SHARE
SHARE
LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIG ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SH GN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIG ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SH
Contents
Contents Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Chapter 1 • What is an Arduino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Chapter 2 • What you need . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 2.1 Arduino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 2.2 Parts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 2.3 Free software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 2.3.1 Free download . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 2.3.2 Arduino IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 2.3.3 HyperTerminal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 2.3.4 PC Oscilloscope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Chapter 3 • Tutorial project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Chapter 4 • The first steps (LED, switch, serial connection) . . . . . . . . . . . . . . . . . . . 23 4.1 Flashing LED . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 4.2 Alternating Flasher . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 4.3 Serial counter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 4.3.1 In the Arduino Serial Monitor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 4.3.2 A nice screen lay-out on the PC without programming . . . . . . . . . . . . . . . . . . . 34 4.4 Serial debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 4.4.1 The sketch doesn’t run correctly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 4.4.2 The result is incorrect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 4.4.3 There is a problem involving multiple variables . . . . . . . . . . . . . . . . . . . . . . . . 38 4.5 We make our own ASCII table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 4.6 Switch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 4.7 Timer with auto-restart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 4.8 Alternating switch (flip-flop) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 4.9 Die . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 4.10 Secret doorbell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Chapter 5 • A/D conversion (var. resistor, LDR, voltages, PWM, sensors) . . . . . . . . . . 61 5.1 Flashing LED with adjustable speed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
●7
45 Arduino Projects UK 160209.indd 7
18-02-16 15:16
Arduino Uno - 45 Projects for Beginners and Experts 5.2 Voltmeter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 5.3 Nightlight (darkness switch) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 5.4 Photometer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 5.5 Children’s bedroom light alarm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 5.6 Variable brightness LED (PWM) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 5.7 Energy Monitor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 5.8 Silent alarm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 5.9 Analyze a transistor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 5.10 The plant needs watering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 Chapter 6 • Measure, control and power (motors, sensors and sound) . . . . . . . . . . . . 97 6.0.1. Power over USB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 6.0.2. An external power supply . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 6.1 Control a motor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 6.2 Variable speed electromotor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 6.3 Tachometer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 6.4 Cruise control (constant speed with feedback) . . . . . . . . . . . . . . . . . . . . . . . . 117 6.5 Infrared object detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 6.6 Ultrasonic sensor (range finder) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 6.7 Tilt- or motion sensor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 6.8 Memory (data storage) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 6.8.1 EEPROM (permanent) memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 6.8.2 Flash (program) memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 6.9 Switch alternating current with a relay . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 6.10 Police siren . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 Chapter 7 • H ow crazy can you get (AI, make your own Arduino, internet) . . . . . . . . . . . . . . . . . . . . . . . . 145 7.1 My favorite color (artificial intelligence, learning program) . . . . . . . . . . . . . . . . 145 7.2 People sensor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 7.3 Electric candle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 7.4 Fencing foil (or epée) tester . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 7.5 Knock knock who is there (knock sensor) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 7.6 Night buzzer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
●8
45 Arduino Projects UK 160209.indd 8
18-02-16 15:16
Contents 7.7 Tetris met 126 LEDs and Charlieplexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 7.7.1 Charlieplexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 7.7.2 Tetris game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170 7.8 Internet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 7.8.1 Check a switch over the internet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 7.8.2 Control an LED over the internet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 7.8.3 Internet versus intranet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 7.9 Wireless control of a relay (radio-link) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190 7.10 Make your own Arduino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 7.10.1 Stand-alone microcontroller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 7.10.2 With a USB connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 7.10.3 Demo project home-made Arduino: flashing LED with adjustable speed . . . . 205 Chapter 8 • Appendix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 8.1 Adjustable power supply (1.2 to 13 volt) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 8.2 The completed shield . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
â—?9
45 Arduino Projects UK 160209.indd 9
18-02-16 15:16
Chapter 4 • The first steps (LED, switch, serial connection))
Chapter 4 • The first steps (LED, switch, serial connection) We will start with a series of projects that make use of LEDs and switches. The first projects are simple, but as we go along the complexity slowly increases. If you don’t want to build the projects, we recommend you read them anyway because we will be discussing and explaining several C++ commands. 4.1 Flashing LED
We will redo the program from the tutorial, but this time we will connect an LED of our own, calculate the current limiting resistor and write the program ourselves. We won’t make use of the LED on the Arduino, but solder one on to the development shield. Or rather two LEDs, one extra for the following project. In order to use an LED we must connect it in series with a resistor. This resistor will limit the current through the LED, hence the name “current limiting resistor”. Without this resistor the LED will be destroyed as will the Arduino port that the LED is connected to.
Figure 12. Section of the datasheet of the LED. The first thing we need to do is look at the datasheet of the LED. The important variable is the current consumption, which in the case of an LED is called “forward current”. The maximum value according to the datasheet is 20 mA, the recommended value is 16-18 mA. The resistor must make sure this value is not exceeded. Apart from the LED, the Arduino must be protected as well. The maximum current that the Arduino can supply through a pin is 40 mA. This value is only applicable when just one pin is using the current. When multiple pins are in use the maximum value drops, and it can be quite complicated to calculate what the maximum is for a given situation. A rule of thumb for the Arduino Uno is that 14 mA is a safe value regardless of the number of pins in use. This is somewhat under the recommended value for our LED but the amount of light it emits at this current is still more than enough. The next important LED property is the voltage drop over the LED. In the datasheet this is
● 23
45 Arduino Projects UK 160209.indd 23
18-02-16 15:16
Arduino Uno - 45 Projects for Beginners and Experts
called “forward voltage”. The average value is 2 volts. That means that if we connect an LED with a resistor in series to 5 volts, the drop over the LED is 2 volts. The drop over the resistor is what is left over, so 5-2=3 volts.
Arduino pin
5 volt
R
5 - 2 = 3 volt
LED
2 volt GND
Figure 13. Voltage distribution over LED and resistor. At this point we know the voltage over the resistor. We also know what current we want to flow through it: 14 mA. Using Ohm’s law we can now calculate the value of the resistor.
V=I*R
With:
V = voltage (in volts) I = current (in amperes) R = resistance (in ohms)
We re-write the formula to R = V / I and enter the data: 3/14 10-3 = 214 ohm. This value doesn’t exist, the standard values are 200 or 220. We select 220 which means the current will be slightly below our goal of 14 mA (in fact the current is 3/220 = 13.6 mA). The color-code of this 220 ohm resistor is red-red-brown. Several parts, including LEDs, have a plus and a minus side.7 If you look closely at the LED you will see that one of the pins ends in a wider area than the other one. This is the side that has to be connected to the minus. Usually the pin on that side is shorter than the other pin. In the schematic the symbol of the LED looks a bit like an arrow. A convenient rule of thumb is that arrows in symbols always point to the minus.
7 Officially the plus or positive side is called anode, and the minus or negative side is called cathode.
● 24
45 Arduino Projects UK 160209.indd 24
18-02-16 15:16
Chapter 4 • The first steps (LED, switch, serial connection))
Figure 14. The plus and minus side of an LED. We now have to select two pins to connect these LEDs to. We will mount them on a shield, and shields have access to all the pins on the four headers of the Arduino Uno. Marking
Meaning
0->RX
Serial receive
1<-TX
Serial transmit
0 t/m 13
Digital in- and outputs
~
Optional: PWM
A0 t/m A5
Analog inputs
Vin
The voltage of your power supply
GND
0 (ground)
5V
+5 volts
8
3.3V (sometimes 3V3)
+3.3 volts
RESET
The reset pin
AREFa
Reference voltage for the analog inputs 9
SCLa
I2C clock
SDAa
I2C data
IOREFa
Indication shield voltage
Table 3. Header connections of the Arduino Uno. 89 The previous table shows the functionality of the different connections on the headers. Some of this may not mean much to you yet, but we have included it anyway so you can use this table as reference for future projects. The pins marked a are available only on certain Arduino Uno versions. We will not use any of them in this book, so it doesn’t matter if your Arduino Uno has them or not. We select two digital pins, one without PWM (pin 2) and one with (pin 5). We will discuss what PWM is, and how we can use it, in project 5.6.
8 Pin 10, 11, 12 and 13 can also be used as SPI bus, pin 2 and 3 can be used as external interrupt. 9 Attention: if you use an external voltage on the AREF pin (minimum 0 and maximum +5 volts) you must give an analogReference command BEFORE issuing an analogRead command, to prevent internal shortage in the Arduino, which may cause irreparable damage.
● 25
45 Arduino Projects UK 160209.indd 25
18-02-16 15:16
Chapter 5 â&#x20AC;˘ A/D conversion (var. resistor, LDR, voltages, PWM, sensors)
Chapter 5 â&#x20AC;˘ A /D conversion (var. resistor, LDR, voltages, PWM, sensors) Certain pins on the Arduino are capable of measuring analog signals and converting them into digital ones. These pins are marked A0 to A5. The analog signal must be a voltage in the range 0 to 5 volts. The result of the conversion is a digital value from 0 to 1023. 5.1 Flashing LED with adjustable speed
We will now make a flashing LED where the speed is adjustable with a variable resistor. For this project we need new hardware: a variable resistor (var). This is a (usually) round component with three pins. The flat var meant for use on printed circuit boards has two pins close to each other. These must be connected to the +5 V and the ground. It is common that turning something to the right means increasing it, or making something louder. For that reason the +5V is usually connected to the right hand side. 22 One of the pins is in the middle, this one is internally connected to the wiper. We will connect this pin to pin A0 of the Arduino. If you turn the var all the way to the left the wiper is flush against the ground pin, which means the voltage on A0 is 0 volt. If you turn the var all the way to the right the wiper is flush against the +5 volt, so A0 is 5 volts. Setting the wiper in between allows you to make any value in the range. A var connected this way is also called a voltage divider.
Arduino Uno
+5V
A0
GND
10k
Figure 27. Connecting a var to the Arduino. On the shield you can connect the var to the wires that run from +5V and ground to the resistors and the LEDs. The wiper is connected to A0.
22 O ddly enough a faucet works the other way: turning it to the left means more water. That is because opening or loosening something is normally done by turning something to the left.
â&#x2014;? 61
45 Arduino Projects UK 160209.indd 61
18-02-16 15:16
Arduino Uno - 45 Projects for Beginners and Experts
Figure 28. The var is mounted on the shield. The result of an analog to digital conversion is a value from 0 to 1023, where 0 represents 0 volts and 1023 represents +5 volts (unless another reference is selected with the analogReference command). Command
Explanation
analogRead(pin)
Reads an analog value of the pin. The answer is an int in the range 0 to 1023.
analogReference(mode)
Set the maximum voltage for analog measurements. Do note: if you do use one you must never exceed it. The following modes are available: DEFAULT = the voltage that powers the microcontroller, so in the case of the ATmega328 microcontroller on the Arduino Uno this voltage is +5 volts. This is also the setting that is selected if you do not use this command at all. INTERNAL = A built-in reference value. For the ATmega328 microcontroller on the Arduino Uno this is 1.1 volts. EXTERNAL = A voltage (in the range of 0 to 5 volts) that you apply to the Aref pin. Warning: if you do use an external voltage on the Aref pin you MUST use the analogReference command BEFORE you use an analogRead command, otherwise the Arduino will short-circuit internally and may be permanently damaged.
Table 15. Analog commands. 23
23 Y ou might have expected the analogWrite() command in this table. This command however refers to a PWM signal and doesnâ&#x20AC;&#x2122;t generate an analog signal at all (so the name is indeed quite misleading). PWM, and also the analogWrite() command, is discussed in section 5.6.
â&#x2014;? 62
45 Arduino Projects UK 160209.indd 62
18-02-16 15:16
Chapter 5 • A/D conversion (var. resistor, LDR, voltages, PWM, sensors)
We will measure the value of the analog pin that the var is connected to. The result is used in a delay command. To make sure that we can actually see the LED flash we add 50 to the measurement, which means that the flashing speed is adjustable from 50 to 1073 milliseconds. int pot = A0; int led = 2; int state = false; void setup() { pinMode(led, OUTPUT); } void loop() { digitalWrite(led, state); state=!state; delay(analogRead(pot)+20); }
We use a single delay statement and use variable “state” to remember if we need to turn the LED on or switch it off. After each flash the state is inverted. 5.2 Voltmeter
In this project we measure the voltage on pin A0 and display it on the PC. We will use the same hardware as in the previous project. The first step is to measure the analog value. In the previous table you have seen that the result is an int, so we declare an int variable called “value” to store the result of the measurement. The next step is to convert the measurement to a voltage. The measurement is a value in the range 0 up to and including 1023, where 1023 represents the reference voltage Aref. The conversion formula is:
Aref voltage = value * 1023
Since we don’t use the analogReference command (and don’t connect anything to the Aref pin), the default value of 5 volts applies, so the formula becomes:
5 voltage = value * 1023
We declare a variable with the name “voltage”. The result of our calculation will have decimals so as variable type we use float. One would expect the following formula to yield the correct result:
● 63
45 Arduino Projects UK 160209.indd 63
18-02-16 15:16
Chapter 6 • Measure, control and power (motors, sensors and sound)
Chapter 6 • M easure, control and power (motors, sensors and sound) In this chapter we discuss projects that use more power than the Arduino pins can supply. If the voltage is different too, then an external power supply must be used. You will find an example of a suitable adjustable power supply in appendix 8.1. What if the current is too high but the voltage is 5 volts, would it be possible to use the +5 volt connection on the Arduino header? In other words: what is the maximum current that this connection can supply? The Arduino Uno documentation is rather vague on this subject, so we consult the schematic.32 The maximum amount of power depends the way that you power the Arduino. 6.0.1. Power over USB
This is the easiest way. You have connected the Arduino by USB cable to your PC, and are not using a separate power supply. In the next Figure we show a small fragment of the Arduino Uno Rev. 3 schematic. If you follow pin 1 of the USB connection (marked XUSB) the wire goes through fuse F1 and stops at USBVCC.
Figure 52. Power over USB: follow XUSB wire. In the next Figure this wire continues via an FDN340P mosfet to the +5 volts (and continues on to the 3.3 volts regulator).
32 T he schematic is part of the free download, but belongs to the Arduino Uno, revision 3. If you own a different Arduino, or a different revision, you need to search for the appropriate schematic and you may need to adjust the calculations in 6.0.1 and 6.0.2.
● 97
45 Arduino Projects UK 160209.indd 97
18-02-16 15:16
Arduino Uno - 45 Projects for Beginners and Experts
Figure 53. Power over USB: follow the USBUCC wire. The only components between the USB connection and the +5 volts pin is a fuse and a mosfet. The fuse is a polyfuse of 500 mA, and this corresponds to the maximum current that a normal USB connection on a PC can supply.33 The FDN340P mosfet accepts a continuous current of 2 A according to the datasheet, so that is high enough, but we should also check the heat dissipation. The FDN340P is not equipped with a heatsink, and according to the datasheet this limits the heat dissipation to PD = 0.5 W. Since the voltage drop over a mosfet is about 0.7 V we can calculate the maximum current with this formula: P=V*I With
P = the power (W) V = the voltage (V) I = the current (A)
Entering our data yields: Imax = PD / V = 0.5 / 0.7 = 714 mA That is more than the polyfuse can handle, so the maximum current is limited to the polyfuse maximum current, which is 500 mA. The Arduino itself needs some of that power as well by the way. As a rule of thumb the Arduino Uno uses about 50 mA. The rest can be drawn from the +5 volts pin. Do note that anything connected to other pins must also be powered from that same 500 mA! 6.0.2. An external power supply
With an external power supply we mean one that is connected to the power supply plug on 33 A polyfuse is an automatic fuse. When the current exceeds a set value the fuse opens, breaking the circuit. Remove the over-consumer and after some time the polyfuse closes again. Unless the current was so large that the polyfuse itself was damaged.
â&#x2014;? 98
45 Arduino Projects UK 160209.indd 98
18-02-16 15:16
Chapter 6 • Measure, control and power (motors, sensors and sound)
the Arduino Uno. An external power supply connected to the USB port is of course covered by the calculation in the previous section. We consult the datasheet again, and note that a NCP1117 regulator converts the voltage from the external power supply to 5 volts.
Figure 54. External power supply: follow the PWRIN wire. When we consult the datasheet of the NCP1117 we read that this regulator can supply “Output Current in Excess of 1.0 A”. That is rather vague, because there obviously has to be an upper limit. The key is the heat dissipation. The regulator has to reduce the voltage to 5 volts, at the current that the Arduino is using. Since a protection diode D1 is used, this drops the voltage by 0.7 volts already.34 The remaining voltage difference is converted into heat by the regulator. The amount of heat (or rather energy) can be calculated with the formula P=V*I. When we enter the output voltage (5 volts) and the voltage drop over the diode (0.7) this is the resulting formula: P = (Vin - 5 - 0.7) * I
(formula 1)
Now the question is how much power the NCP1117 can dissipate (PD). We consult the datasheet once again, see the next Figure.
34 The typical voltage drop of a diode is 0.7 volts.
● 99
45 Arduino Projects UK 160209.indd 99
18-02-16 15:16
Arduino Uno - 45 Projects for Beginners and Experts
Figure 58. The new components on the shield, top- and bottom view. In the sketch we use two switches to control the motor. Switch one (on pin 8) starts and stops the motor, and pin 2 (on pin 11) changes the direction. We use two variables, state and dir, that the switches flip from true to false and back. // run-stop if (digitalRead(button1) == HIGH) { state=!state; while (digitalRead(button1) == HIGH) { delay(20); } } // forward-reverse if (digitalRead(button2) == HIGH) { dir=!dir; while (digitalRead(button2) == HIGH) { delay(20); } }
Once that is completed the new states are processed. The completed sketch is as follows: int button1 = 8; int button2 = 11; int motor1 = 10; int motor2 = 13; int state=false, dir=false; void setup() { pinMode(motor1, OUTPUT);
â&#x2014;? 104
45 Arduino Projects UK 160209.indd 104
18-02-16 15:16
Chapter 6 â&#x20AC;˘ Measure, control and power (motors, sensors and sound)
pinMode(motor2, OUTPUT); pinMode(button1, INPUT); pinMode(button2, INPUT); } void loop(){ // run-stop if (digitalRead(button1) == HIGH) { state=!state; while (digitalRead(button1) == HIGH) { delay(20); } } // forward-reverse if (digitalRead(button2) == HIGH) { dir=!dir; while (digitalRead(button2) == HIGH) { delay(20); } } // execute commands if (state==true){ digitalWrite(motor2, dir); digitalWrite(motor1, !dir); } else{ digitalWrite(motor2, LOW); digitalWrite(motor1, LOW); } }
The sketch can be controlled as follows: 1. Make sure that pins 10 and 13 are low whenever the TC4427A is not powered (meaning that Vdd is not connected). 2. In almost every case you will need to use an external power supply for your motor. In appendix 8.1 a schematic is shown for the power supply that has been used for the projects in this book. Connect the GND of the power supply to the GND on the 6-pin block on the shield, and the Vdd on the appropriate pin on the TC4427A (see the next Figure). 3. Connect the motor to the pins near the TC4427A (M1 and M2), and switch the external power supply on. 4. Put the sketch into the Arduino. It is possible that the motor rocks back and forth a bit during programming, this is normal.
â&#x2014;? 105
45 Arduino Projects UK 160209.indd 105
18-02-16 15:16
Chapter 7 • How crazy can you get (AI, make your own Arduino, internet)
Chapter 7 • H ow crazy can you get (AI, make your own Arduino, internet) A small collection of bizarre sketches and weird applications. 7.1 My favorite color (artificial intelligence, learning program)
In this project we build a sketch that learns which color you like best, yellow or green, and humors you by predominantly lighting that LED. In 1995 an article written by Watanabe was cause for commotion. 51 It appears that he had trained pigeons to distinguish between paintings by Picasso and Monet, a feat previously assumed to be impossible. The pigeons received a reward if they pecked a switch, but only if a painting by Picasso was shown at the same time. Initially the pigeons pecked completely at random. After a while however they stopped pecking if a Monet painting was shown, and only pecked if a Picasso was shown. In this project a sketch in the Arduino randomly switches a yellow or green LED on. You can “reward” the Arduino for one of these colors by pressing a switch. After such a reward the Arduino will be more inclined to light that particular LED. Eventually the Arduino learns that you like that color best, and will only light that LED every single time. The sketch learns just like the pigeons in the research project. Does that mean that this sketch has artificial intelligence? The behavior of the pigeon and the Arduino is identical, so one could answer this question in the affirmative. Pigeon brains however function completely differently from the Arduino program, so the answer could also be negative. But nobody knows how pigeon brains work exactly, so does it even matter? Since this is a book on Arduino’s and not philosophy, we simply state that a program that behaves intelligently actually is intelligent. The internal difference between brains and the sketch is then attributed to the difference between biological and artificial intelligence. In this project an Arduino randomly lights a green or yellow LED. We do this by throwing a “die”.52 If the throw is less than a certain value (a variable called “choice”) then the yellow LED is lit, otherwise the green LED is lit. // throw the die select = random(1,10); // compare to choice if (select<choice){ digitalWrite(yellowled,HIGH); yellow=1; } else{ 51 Watanabe, S., Sakamoto, J., & Wakita, M.: “Pigeon’s discrimination of paintings by Monet and Picasso”, Journal of the Experimental Analysis of Behavior 63 (1995), pp. 165-174 52 The die has values from 1 to 10 (so 1 up to and including 9). Dungeons & Dragons fans will recognize this as the famous d10 die.
● 145
45 Arduino Projects UK 160209.indd 145
18-02-16 15:16
Arduino Uno - 45 Projects for Beginners and Experts
digitalWrite(greenled,HIGH); yellow=0; }
You can “reward” the Arduino for the selected color by pressing the switch. The result of this reward is that the sketch will increase the chance of selecting this color by adapting the value of variable choice. if (digitalRead(button) == HIGH) { // reward current behavior if (yellow==1 and choice<=9){ choice++; } if (yellow==0 and choice>0){ choice--; }
If you press the button while for example the yellow LED is lit, then the value of choice is increased by one. The result is that the chance of selecting yellow on the next throw is increased because it is easier to throw a value less than choice. The value of choice cannot be larger than nine because the highest throw is nine. The same applies for the green LED, except that the value of choice is now decreased. And of course choice cannot be smaller than zero. The result of repeated rewards is that choice is pushed to its boundary value, and the Arduino picks one single color. int button = 8; int greenled = 2; int yellowled = 5; int choice=5, select, yellow; void setup() { pinMode(greenled, OUTPUT); pinMode(yellowled, OUTPUT); pinMode(button, INPUT); randomSeed(analogRead(5)); } void loop(){ // therow the die select = random(1,10); // compare to choice if (select<choice){ digitalWrite(yellowled,HIGH);
● 146
45 Arduino Projects UK 160209.indd 146
18-02-16 15:16
Chapter 7 • How crazy can you get (AI, make your own Arduino, internet)
yellow=1; } else{ digitalWrite(greenled,HIGH); yellow=0; } // wait of a reward, if any for (int t=0;t<100;t++){ if (digitalRead(button) == HIGH) { // reward current behavior if (yellow==1 and choice<=9){ choice++; } if (yellow==0 and choice>0){ choice--; } // wait for the switch to be released while (digitalRead(button) == HIGH) { delay(20); } } // give the user a bit of time delay(20); } // switch the LEDs off digitalWrite(yellowled,LOW); digitalWrite(greenled,LOW); delay(200); }
As hardware you need the Arduino, two LEDs and a switch, so the setup we made in chapter 1. When you run the sketch one of the LEDs will be lit. Pick a color, and press the reward button whenever that LED is lit. After a while that will be the only LED that the Arduino will switch on. Optional Once the sketch has learned its lesson you cannot change your favorite color because the other color is never shown. Why don’t you try to change the sketch so that changing your preference is possible? Hint: You can use a second button to punish the sketch for the wrong color. If you have these two switches you can also see why inconsistent rewarding and punishing doesn’t lead to a satisfactory result, for example when you sometimes reward for a color, and then punish for that same color. This doesn’t just apply to “raising” your Arduino, but for raising children as well.
● 147
45 Arduino Projects UK 160209.indd 147
18-02-16 15:16
Chapter 8 • Appendix
Chapter 8 • Appendix 8.1 Adjustable power supply (1.2 to 13 volt)
The adjustable power supply in this appendix supplies 1.2 to 13 volts, and is suited for all projects in this book that need an external power supply. If you add a 1.6 inch2 heatsink the maximum current is about 1.5 A. 3
LM317 1
+1.24 - 13.5 V (out)
2
470
+ 15 V (in)
100 nF 5 k LIN
+ 100 uF
0
Figure 135. Schematic of the adjustable power supply.
Figure 136. Pin lay-out of the LM317. You will need a voltage meter to determine the position of the variable resistor for each voltage. If you put this project in a nice case with a scale next to the variable resistor you only need to calibrate it once. The project is powered by a 15 volt DC plug (a “wall wart”). If you want to use a different voltage do read the explanation carefully because you will need to use different component values. If you want to use an AC power supply you will need to add a rectifier to convert it into DC.
● 207
45 Arduino Projects UK 160209.indd 207
18-02-16 15:16
Arduino Uno - 45 Projects for Beginners and Experts
Figure 137. Power supply in a case. The LM317 is a 1.2 volt voltage regulator. This means that the voltage drop over the LM317 itself is always 1.2 volts. The maximum voltage that we shoot for is 13 volts, which should be attainable with a 15 volts DC supply. We use a 5 k LIN variable resistor to control the voltage. That means that the voltage drop over the var must be 11.8 volts (13 - 1.2) and that means that the current flowing through it is 2.36 10-3 amp, because: Vmax = 13 Vvar = 13 - 1.2 = 11.8 volt Ivar = V/R = 11.8 / 5 k = 2.36 10-3 amp This current doesn’t flow through the LM317 so we need a bypass resistor. Since the voltage drop over the LM317 is 1.2 volt, and we just calculated the current, the value of this fixed resistor can be easily calculated: Rfix = V/I = 1.2 / 2.36 10-3 = 508 ohm. That value doesn’t exist, we can choose between 470 and 560 ohm. We wouldn’t mind a slightly higher maximum voltage so we select 470 ohm (yellow-purple-brown). Should you wish to add an LED, so you can easily tell if the power supply is on, you should use a 1k resistor (brown-black-red) and connect it to the 15 volt supply side. 8.2 The completed shield
The next Figure shows what the completed shield that we have made in this book looks like. It also shows the function of the different pins. You will find this Figure in the download as well, so you can print it and keep it as a reference with the shield. Pins M1 and M2 are the outputs and Vdd the motor-voltage-input of the TC4427A mosfet driver. Pins R1 and R2 are the relay contacts.
● 208
45 Arduino Projects UK 160209.indd 208
18-02-16 15:17
Index
Index Symbols * 51 + 51 - 51 != 51 ** 51 / 51 % 51 < 51 51 << <= 51 = 51 > 51 >= 51 51 >> -- 33 -= 33 ! 31 [ 35, 41 *= 33 /= 33 \ 44 & 109, 136 ++ 33 += 33 << 168 == 51 109 | 1N4007 139 71427 101 207 LM317 \n 45 \n\r 45 A analogRead 62 analogReference 62 analog - voltage 63 and 109 Arduino 11 Arduino IDE 14 array 57 artificial intelligence 145
ASCII 34, 43 ATmega328P 11, 13, 196 auto-calibrate 73, 78 avr/pgmspace 134 B baud 181 baudrate 32 BC547B 152 BC547C 88 BD679A 92 BJT 88 141, 159 buzzer byte 29 C candle 150 char() 193 charlieplexing 160 Charlieplexing.h 166 clamping diode 139 close 181 compile 21 crystal 11 CS22 CS20 108 D delay() 28 deplace 17 die 55 digitalRead 49 digitalWrite 27 DIP05-1A72-12L 138 driver 19 duty cycle 74 E echelle 17 EEPROM 129 EEPROM.read 130 EEPROM.write 130 else 50 epée 153
● 211
45 Arduino Projects UK 160209.indd 211
18-02-16 15:17
Arduino Uno - 45 Projects for Beginners and Experts
F F() 137 favicon 176 Flash 133, 167 float 29 fly back diode 139 foil 153 for 45 forward voltage 23 G GP1S036HEZ 124 GP2Y0D340K 118 H header pins 25 hFE 92 HIGH 27 HyperTerminal 14, 36 hysteresis 69 I Ic 92 if 50 include 130 index 57 indexOf 193 int 29 176 IP adres J 143 James Boyk L LDR 67 LED 23 LedSign 166 LOL 166 loop() 27 LOW 27 lux 67 lux calculation 70 M Magnetic switch map max current
83 75, 110 101
MOSFET driver
101
N not 31 noTone 144 O Ohm's law 24 open 187 optiLoader 199 or 109 OUPUT 27 P people sensor 148 pgm_read_word_near 136 piezo 141, 157 pinMode 27 pointer 136 port 181 port forwarding 190 power 97 powersupply 207 prog_ 135 prog_char 136 prog_int16_t 136 prog_int32_t 136 PROGMEM 135, 168 prog_uchar 136 prog_uint16_t 136 prog_uint32_t 136 Ptot 92 pull-down 47 pull-up 47 Pulswiel 113 P=V*I 98 PWM 74, 107, 150 PySerial 173 Python 173 Q QRB1134 113 R radio 190 random 55, 150
â&#x2014;? 212
45 Arduino Projects UK 160209.indd 212
18-02-16 15:17
45 PROJECTS FOR BEGINNERS AND EXPERTS Bert van Dam
ISBN 978-1-907920-49-3
You can use it as a projects book and build more than 45 projects for your own use. The clear explanations, schematics, and pictures of each project make this a fun activity. The pictures are taken of a working project, so you know for sure that they are correct. You can combine the projects in this book to make your own projects. To facilitate this, clear explanations are provided on how the project works and why it has been designed the way it has. That way you will learn a lot about the project and the parts used, knowledge that you can use in your own projects.
45 PROJECTS FOR BEGINNERS AND EXPERTS
ARDUINO UNO
● BERT VAN DAM
Bert van Dam is a freelance author of project books, courseware and articles on microcontrollers including ARM and PIC devices. In his publications he also covers Raspberry Pi, artificial intelligence and several programming languages like JAL, C, assembler, Python, and Flowcode.
This book covers a series of exciting and fun projects for the Arduino, such as a silent alarm, people sensor, light sensor, motor control and an internet and wireless control (using a radio link). Contrary to many free projects on the internet, all projects in this book have been extensively tested and are guaranteed to work!
ARDUINO UNO – 45 PROJECTS
ARDUINO UNO
Apart from that, the book can be used as a reference guide. Using the index, you can easily locate projects that serve as examples for the C++ commands and Arduino functionality. Even after you’ve built all the projects in this book, it will still be a valuable reference guide to keep next to your PC. LEARN
www.elektor.com
DESIGN
Elektor International Media BV
Bert van Dam LEARN
DESIGN
SHARE
SHARE
LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIG ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SH GN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIG ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SH