© Jonathan Bartlett 2020
J. BartlettElectronics for Beginnershttps://doi.org/10.1007/978-1-4842-5979-5_14

14. Building Projects with Arduino

Jonathan Bartlett1 
(1)
Tulsa, OK, USA
 

Chapter 13 covered the basics of what microcontrollers are, what the Arduino environment is, and how to load a program onto an Arduino board. In this chapter, we will go into more depth on how to include an Arduino Uno into a project.

14.1 Powering Your Breadboard from an Arduino Uno

The first thing to understand about the Arduino Uno is that one of its main jobs is power regulation. As we saw in Chapter 13, the Arduino can use a variety of power sources—USB, battery, or a wall plug. Additionally, there is a connection on the Uno’s headers that allows for you to supply power from some other source.

If you have a power source that just has wires coming out of it—like a 9 V battery with a simple wire connector—the Uno has a place that you can plug it in. The pin labeled VIN is used for supplying an unregulated voltage supply (7–12 volts) to the Uno (do not use the one labeled 5 V—it is for outputting power). Therefore, if you plug the positive wire into VIN and the negative wire into any of the GND pins (it doesn’t matter which one), then the Uno will spring to life.

On the flip side, you can actually power the rest of your project from the Uno and take advantage of its voltage regulation, as well as its numerous methods for getting power. To do this, simply take a wire from the 5 V connection on the Uno and connect it to the positive rail on your breadboard. Then, take another wire from one of the GND connections on the Uno and connect it to the ground rail on the breadboard. Voilà! A very flexible 5 V power supply for your breadboard. Figure 14-1 shows how to use this to light a simple LED circuit.
../images/488495_1_En_14_Chapter/488495_1_En_14_Fig1_HTML.png
Figure 14-1

Powering a Simple Project from an Arduino Uno

Also note that there is also a 3.3 V connection if you need it, as many small devices are powered at that level.

14.2 Wiring Inputs and Outputs to an Arduino Uno

Now that we know how to power a breadboard from an Arduino Uno, we can now see how to connect inputs and outputs to the Uno.

Wiring inputs and outputs to an Uno is actually very easy. Outputs of the Uno can be viewed as simple voltage sources, like a battery, which operate at either 5 V (if set to HIGH) or 0 V (if set to LOW). Remember that any of the digital I/O pins can be set to be input or output pins in your Arduino program with the pinMode command .

However, even though an output pin can act as a voltage source, the current needs to be limited to prevent damage to the Arduino. Each output pin should only be sourcing up to 20 mA of current, and the total amount of output of all pins combined should never exceed 100 mA. So, for instance, if you have an LED output, be sure to add a resistor to limit the amount of current. Microcontrollers generally cannot drive high-power devices such as motors directly and must rely on some method of amplifying the signal after it leaves them (we will cover amplification in Part 4 of the book).

Inputs to an Arduino are essentially voltage sensors. They will detect a HIGH (around 5 V) or LOW (near 0 V) signal on the pin. You can think of them as having a very large resistor attached to them (about 100 MΩ—100 million ohms), so they don’t actually use up any serious amount of current (in other words, you don’t have to supply a current-limiting resistor because it is already built in). However, because they use so little current, that means that, just like our inputs in Chapter 12, they cannot be left disconnected, or the results may be randomized from static electricity in the air. Thus, for inputs, you should always attach a pull-up or a pull-down resistor (usually a pull-down) to the input to make sure that the input is always wired into the circuit in a known-valid state.

14.3 A Simple Arduino Project with LEDs

In this section, we are going to look at making a simple Arduino project with two buttons, each controlling one of two LEDs. This would actually be simpler to wire without the Arduino, but the goal is to make a baby step to understanding how Arduino projects work. Later we can do more complicated things, but, for right now, we will just see how we get an input signal to the Arduino and send an output signal back out.

This project is going to have buttons wired into Digital Pin 2 and Digital Pin 3 of the Arduino and LEDs wired into Digital Pin 4 and Digital Pin 5. Let’s think about what these need to look like. The LEDs will each need a current-limiting resistor, and the buttons will each need a pull-down resistor.

Figure 14-2 shows how this should be wired up. The breadboard is being powered from the 5 V and GND terminals on the Arduino. The wires on the right side make sure power is connected to both sides of the breadboard. On the bottom, buttons are wired up with pull-down resistors and connected to Digital Pins 2 and 3 on the Arduino. On the top, the LEDs are connected to Digital Pins 4 and 5, with current-limiting resistors making sure they don’t draw too much current.
../images/488495_1_En_14_Chapter/488495_1_En_14_Fig2_HTML.png
Figure 14-2

Wiring a Simple Button-Based Arduino Project

Now, of course, for an Arduino, this is not enough. The Arduino also needs a program to control it! Figure 14-3 shows the program you will need to type in to control the LEDs.
../images/488495_1_En_14_Chapter/488495_1_En_14_Fig3_HTML.png
Figure 14-3

An Arduino Program to Control Two Buttons and Two LEDs

Note that, as usual, the project is divided into two pieces—setup(), which only occurs once when the chip starts up, and loop(), which continuously runs over and over again until the chip is turned off or reset:

setup() simply tells which pins should be in which mode. Note that, unless it includes the calls to the delay() function, the loop() function will literally run thousands of times per second (or more). The Arduino Uno can execute approximately 16 million instructions per second. Each line of code translates to many instructions, but nonetheless, it goes really fast. Just keep this in mind when you are writing programs.

Inside the loop() function , we have a new Arduino function—digitalRead(). The digitalRead function takes a pin number and returns whether that pin is HIGH or LOW. We have put this into a conditional—if the read from the button pin is HIGH, then the corresponding LED pin is turned HIGH. Alternatively (i.e., else), if the read from the button pin is not HIGH (i.e., it is LOW), then the corresponding LED pin is turned LOW. Note that there are two equal signs used in the comparison. In many programming languages, you use two equal signs to tell the computer to compare values. A single equal sign often means that you are setting a value (not comparing them).

This book is not a book on computer programming, so we are not going to cover all of the details. Because of this, most of the programs will be given to you, and you will only need to make minor modifications. However, if you are interested in learning more, the programming language being used in the Arduino environment is C++, and the Arduino focuses on the easier-to-understand portions of it. The book Beginning Arduino Programming by Brian Evans is a good place to start. If you want to learn more about programming in general (not related to Arduino), you can check out my own book, New Programmers Start Here. If you want to understand better how programming works from the perspective of the computer, you can check out my book Programming from the Ground Up.

14.4 Changing Functionality Without Rewiring

Now, you might reasonably be thinking, Wouldn’t this be a lot easier if we just directly attached the buttons to the LEDs to turn them off and on? Indeed, it would. However, by having the inputs and outputs all wired to the Arduino, we can actually change the functionality of the project without having to do a single bit of rewiring! For instance, if we wanted to have the left button control the right LED and the right button control the left LED, then all we would have to do is swap all of the 2s for 3s in the program and vice versa.

Now imagine if we had spent time developing such a device and even had it sent off to manufacturing, but later decided that we wanted to change the functionality. If all of the logic of the device is implemented by hardware, then that means that you have to throw away all of your old inventory to modify your functionality. If, instead, you use software to connect your components, then oftentimes you can update your device merely by updating your software.

Other modifications we can think of to this simple device might include the following:
  1. 1.

    Making the LEDs turn off rather than on when the buttons are pressed

     
  2. 2.

    Making the LEDs blink when the buttons are pressed

     
  3. 3.

    Changing the buttons to be simple toggles, so that you don’t have to keep on holding the buttons down to keep the LED on

     
  4. 4.

    Requiring that the buttons be pushed in a particular order in order to turn on the LED

     
  5. 5.

    Requiring that both buttons be pushed to turn on the LEDs

     

This list could go on and on. By routing all control processing through your microcontroller, you make your devices much more flexible. Additionally, at some point, they also become cheaper. When mass-producing, microcontroller chips like the ATmega328/P can be had for just over a dollar. Some chips, when purchased in bulk, cost less than 50 cents! So, if a microcontroller is replacing a complex sequence of logic gates and other control functionality, moving all of your control logic to a microcontroller can actually be much less expensive than hardwiring it, and you get added flexibility as a side bonus.

14.5 Review

In this chapter, we learned the following:
  1. 1.

    You can power your breadboards using the 5 V and GND pins on the Arduino to simplify power regulation in your projects.

     
  2. 2.

    Input pins on the Arduino are high-impedance inputs (they act like they have a very large resistor attached, so they don’t eat a lot of current).

     
  3. 3.

    Because input pins use so little current, buttons need to be sure they have a pull-up or pull-down resistor attached to make sure the input is always detecting real voltage values and not static electricity in the air.

     
  4. 4.

    Care has to be taken when using output pins to make sure the current is never too much for the chip.

     
  5. 5.

    Using a microcontroller allows you to rewrite the logic of the project without changing the wiring.

     

14.6 Apply What You Have Learned

  1. 1.

    Which Arduino pin do you use when supplying an unregulated voltage (i.e., a voltage above the 5 V that the Arduino runs at)?

     
  2. 2.

    What Arduino pins would you use to extract power out from an Arduino connected to a power supply?

     
  3. 3.

    What is the voltage of an output pin set to HIGH?

     
  4. 4.

    What is the maximum current that should be sourced by any particular Arduino pin?

     
  5. 5.

    If you have a red LED attached to an Arduino output pin, what is the minimum size of a resistor that you need?

     
  6. 6.

    If an Arduino input pin is completely disconnected from a circuit, what state does the Arduino read it as?

     
  7. 7.

    How much current does an Arduino input use?

     
  8. 8.

    What is the best way to wire a button to an Arduino?

     
  9. 9.

    What is an advantage of storing a program in a microcontroller even if the logic could be built directly in hardware?

     
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
54.226.226.30