Introduction: Arduino and Circuitry
Building circuits with multiple components is an important skill that will be very useful in EGR101 and future engineering classes at Duke. Circuits with an Arduino can be used for anything from robots to home automation to sensing and data collection.
In this Instructable, we will be taking a look at how to build a simple circuit with an LED (Light Emitting Diode) and a button. The process of building this circuit will help you learn the basics of circuit building with an Arduino.
The first project we'll build is a simple circuit that can blink an LED on and off for intervals of 1 second. Then, we'll be modifying that circuit to incorporate a button.
While example code and diagrams are given to you for this Instructable, it is important to use the examples constructively. Try to fully understand each example in order to learn the most you can about how to draw circuit diagrams, build circuits, code in the Arduino IDE, and test or troubleshoot your circuits.
Step 1: Safety
Before beginning to work with any electronics, it is important to follow these safety guidelines to prevent injuries to yourself or the equipment:
- Tie back any loose hair.
- Wear appropriate clothing for the job, such as closed toe shoes.
- Remove any dangling jewelry.
- Wear safety glasses.
- Always work with a partner in case an accident happens.
Also, make sure to follow these specific guidelines when working with sensitive electronics:
- Discharge static electricity by touching a metal surface before working with sensitive electrical components
- Always double check the placement and orientation of a component before inserting it into a circuit. Connecting a component incorrectly can be dangerous or break the component.
Step 2: Blinking LED Circuit
We will start this Instructable by beginning to learn about circuits with an Arduino Uno. We will do this by building a very simple circuit that blinks an LED at intervals of 1 second. A video of the circuit working is shown above. The followings steps in this Instructable will walk you through the process of building this circuit.
Step 3: Downloading the Arduino IDE
Before we begin the process of building circuits with Arduinos, we have to have the proper software to program the Arduino.
To begin working with Arduino on your computer, download the Arduino IDE (Integrated Development Environment, a piece of software used to program the Arduino) from the main Arduino website at https://www.arduino.cc.
On the main website, go to "Downloads" under the "Software" tab at the top.
On the "Downloads" page, scroll down until you see sections titled "Download the Arduino IDE". From there, download the Arduino IDE for your respective system.
Once downloaded, plug in the Arduino to your computer and open the IDE. The program should look like the first image above for Windows or the second image above for Macs. The only main difference from how your program will look like and how it looks in the images above will be the Arduino file name. This is because the default file name when you create a new file is based on the date that you created the file (e.g. the file in the first image was created on May 15th, so the file name is named sketch_may15a). As a result, your default file name will most likely be different from the images above.
Step 4: Configuring the Arduino IDE
Before we can use the Arduino IDE, we must make some changes to the settings in order to be able to upload code to an Arduino.
Plug in the Arduino to your computer with a USB cable and then open up the Arduino IDE.
The settings that need to change can be found under the Tools tab in the menu bar. First, change the Board setting so that the IDE is uploading code to the correct board type. When you open the tab for boards, a list of different types of Arduinos will open up. For this Instructable, we will be using the "Arduino/Genuino Uno" board, so select that option (see the first image for reference).
After selecting the board, you will need to select the correct port. The Port tab is found directly underneath the Board tab. Select the port with the Arduino Uno indicator next to it (see the second image for reference).
With this configuration done, you are ready to program an Arduino Uno!
Step 5: Sketching a Circuit Diagram/Schematic
With your Arduino configured, we are prepared to begin writing our code and building our circuit. However, before we can begin that, we need to figure out exactly what we need to do. To do this, we need to make an electronic or hand drawn schematic of the circuit first.
A circuit diagram/schematic is a drawing that shows how your circuit is going to connect together. This schematic will act as the blueprint for building the physical circuit and the Arduino code and serve as the starting point for any necessary circuit troubleshooting.
Follow these steps to draw your circuit diagram:
- Determine which components you plan on using in your circuit. In our example, we're using an LED, a resistor, and an Arduino.
- Develop an understanding of how you need to connect your components to achieve the desired result. On a piece of paper, state the desired function of the circuit, then try to list all the steps needed to achieve that result. For example, to make an LED blink, you need an Arduino to be powered. Then, you need to switch power to the LED using the Arduino on, then off, repeatedly.
- Sketch out how each component must be connected together to carry out these steps. To make the LED light up, we must provide power to the LED (we flow a current through the LED). To control the light from the LED turning on and off, we can use the Arduino to turn the current to the LED on and off. So, the LED must be connected to a control pin on the Arduino. To limit the current to protect the LED, we will use a resistor between the Arduino and the LED. The LED must also be connected back to Ground on the Arduino to complete the circuit.
Use schematic symbols to represent each component and draw solid black lines to represent wired connections. For more information about circuit diagram symbols for the different components, see this link: https://learn.parallax.com/support/reference/sche...
As you complete these steps, you might need to:
- Specific circuit elements need to be used in a specific manner. For example, an LED is a diode, and current can only flow through it in one direction to make the LED emit light. This means it can be only connected in one direction (or polarity). The two connections to the LED are called a cathode and an anode. The anode is connected to the positive terminal of a power supply (in our case, the Arduino), and the cathode to ground (or negative supply).
- You might also have to consider what components need to be used together. For example, LEDs must be connected in series with a resistor to protect the LED because the LEDs use a lower current flow than the Arduino outputs. For this project, 220 ohms is a good resistor value.
- Look up the different functions of pins on an Arduino board. One of the main features on an Arduino board are the GND and power pins. The GND pin represents ground or 0V and the power pins (5V and 3.3V) represent pins that output a constant voltage. Another one of the main features on the Arduino board is the control pins on the Arduino, which are pins that can control an output or input signal. For example, there are 12 digital pins denoted on the Arduino board. Digital pins mean that they can only send a HIGH (+5V) or LOW (ground or 0V) digital signals, while analog pins on the Arduino board can send analog signals, meaning that they can output a signal with any voltage value between 0V and +5V. The components used in this Instructable will be using digital pins 1-12, and there is no difference between the 12 digital pins. As long as your Arduino code accurately reflects the pin that you chose, your circuit will work with your code. Read up on this topic or ask for help if you need help understanding the different parts of an Arduino.
A good circuit drawing needs to be neat, have circuit elements clearly labelled with their appropriate schematic symbol, and have any necessary values for each circuit element labelled.
The example circuit diagram above is a correct way to draw the LED circuit that you should build. As seen in the diagram, the signal (current) from the Arduino will exit from digital pin 12, goes through a 220 Ohm resistor into the anode end of the LED, exits out of the cathode end of the LED, and then back into the ground pin of the Arduino.
Step 6: Designing Your Circuit on TinkerCAD
After finishing your schematic, it is very helpful to design your circuit on TinkerCAD. TinkerCAD is a program that provides a realistic view of an Arduino and breadboard in order to help you plan exactly how to wire your circuit. (Note: In past semesters of EGR101, we used a software called Fritzing instead of TinkerCAD. So, in this Instructable, there may be some references/pictures that showcase the Fritzing software; however, these pictures can still be used as references when using the TinkerCAD program.)
When prototyping how to wire a circuit, you will want to use a breadboard. A breadboard is a tool that can be used to quickly and easily wire circuit prototypes. In the third image above, the image shows a breadboard and what the inside of a breadboard looks like. It contains a grid of holes, into which you can insert electrical components and wires. In a breadboard, rows of holes are usually connected together by strips of metal. Look at the third image above where the inside of a breadboard is shown. The rows in the middle of the breadboard are connected horizontally are the main building site for components like LEDs and resistors. The columns, or rails, on the edges of the breadboard are connected all the way across the board by the long narrow strips of metal denoted by the red arrows in the image. These columns on the breadboard are usually reserved for connecting the power source and ground because they run along the whole length of the breadboard. Thus, the breadboard allows us to make quick and easy solderless connections when prototyping circuits.
While we could go directly to wiring on a physical breadboard, it is often useful to use software like TinkerCAD to design a circuit on an electronic breadboard before physically making it. You can access TinkerCAD at the following link: https://www.tinkercad.com/learn/circuits/learning
After accessing TinkerCAD, you should wire your circuit on TinkerCAD based on your circuit schematic that you made before. If you need help creating the blinking LED schematic in TinkerCAD, you can reference the first picture on this Instructable page.
It is imperative that you learn how to draw a schematic before building a physical circuit because it is a common standard of practice for engineers in industry. It will also help tremendously as you begin to build your physical circuit.
Step 7: Building Your Circuit on a Breadboard
After making your schematic and the TinkerCAD model, you can begin to build your circuit on a physical breadboard. To begin building, gather the materials needed to make the circuit. This includes the following:
- A breadboard
- Jumper Wires
- An LED
- A 220 Ohm Resistor
- Arduino Uno
Again, breadboards are a quick and easy way to connect circuits. They have a grid of holes into which you can insert circuit elements and create a prototype circuit. They have conductive strips of metal that transmit current across multiple holes. In the picture above, note how the middle holes in the breadboard conduct current across the rows, while the outer holes conduct current in a column along the rail.
To determine the resistance of a resistor, refer to the resistor's color bands. The color bands form a code that communicates the resistance. For example, the 220 Ohm resistor has a color band pattern of red, red, brown, gold. A good way to check the resistance of a resistor is to use a multimeter to physically measure the resistance found in the resistor, or you can use an online resistor calculator (such as the one here: https://www.digikey.com/en/resources/conversion-calculators/conversion-calculator-resistor-color-code-4-band) to find the resistance value of a color banded resistor.
Before building the circuit, make sure the Arduino is turned off by unplugging any power sources connected to the Arduino, including your computer. Then, by following the TinkerCAD model and the circuit schematic, build the circuit onto the breadboard with the materials you gathered. Be sure to insert the LED correctly by connecting the anode (the positive end of the LED) into the row with the resistor and the cathode (the negative end) to the ground wire. The anode is denoted by the longer lead of the LED and the cathode is denoted by the flat side on the LED's casing.
After building the circuit as seen on TinkerCAD, you may move on to the next step.
Step 8: Writing the Arduino Code
After building the circuit, all there is left to do is write the code on the Arduino IDE and upload it!
In the images above, you can see working example code for the LED circuit with comments and proper attribution. This code was modified slightly from the Blink.ino example from Arduino to work with the circuit we made by using the digital pin 12 of the Arduino. While we provide you with this example code, it is still important for you to learn the specific syntax of Arduino coding that went into the example code so that you will be able to write your own code in the future.
The code uses a constant integer value so that the LED pin can be changed easily. The main functions used in this code are digitalWrite(), pinMode(), and delay(). You can infer what these functions do based on the comments, but it would beneficial to read more about Arduino syntax and different Arduino functions at https://www.arduino.cc/reference/en/ so that you can create your own code in the future.
Keep in mind that when creating code, you must comment your code and provide code attribution at the top. These comments should be concise and provide information about what your code is doing. This allows a reader to know what your code is doing on a high level and shows that you understand how your code is working. There are two types of comments: line comments and block comments. To make a line comment, use two forward slashes ("//") and then write your comment after the two forward slashes. Any text that you write on the line after the two forward slashes will not be recognized by the Arduino compiler but is still readable by other programmers looking at your code. Line comments can be seen in the second picture with line comments describing what the code does. A block comment is a comment that can span multiple lines and is created by writing a comment that spans multiple lines, with the comment starting with a forward slash and asterisk and ending in an asterisk and forward slash (/* comment */). The first picture shows a block comment being used for the attribution of the code.
For attribution, if you used code from an online resource or from an example with permission, it is important to give credit to the source of your code. To attribute code, use a block comment at the top of the code file with the purpose of your code, the source of your code (include a link if possible), the date that you modified it, and who modified it. See the examples above for reference.
Copy the code and upload this code onto the Arduino by verifying and uploading with the two circle buttons in the top left of the Arduino IDE. If your circuit behaves correctly, turning the LED on for one second and then turning it off for one second repeatedly, like in the video above, you may continue on to the rest of this step. If not, troubleshoot to figure out what went wrong with your circuit. To troubleshoot, start with your schematic, then your TinkerCAD model and physical circuit, and then your code.
After verifying that your circuit works correctly with the Blinking_LED.ino file (there is a video of a Blinking_LED circuit in the images above), try experimenting with the LED circuit to make it do the following:
- Make the circuit blink the LED, but instead of pin 12, use pin 6 instead.
- Make the circuit blink the LED with intervals of 5 seconds instead of 1 second.
Once you successfully change the code/wiring to make the circuit perform the actions above and understand why, change the code back to the original example code and move on to the next step to learn how to add a push button into the system.
Step 9: Modifying the Circuit to Add a Push Button
In this step, we will be adding a push button to the circuit so that the LED turns on when the button is pressed. We will need to modify how the code and circuit works in order for this behavior to be achieved.
A push button is a simple mechanical switch used in circuitry, often with a round button that can be pressed. These buttons often come with 4 little metal legs that curl and point inwards to the body of the button. Legs that are directly across from each other and point towards each other are a leg pair. The legs in a leg pair are always connected to each other in a button, and the 2 leg pairs on a button can be connected together based on if the button is press of not. When the button is not pressed, the switch is normally opened, so path between the leg pairs is broken, and no current can flow between them. When the button is pressed, the switch is closed, so the path between the leg pairs is connected, and current can flow between the two leg pairs.
To add a button to the Blinking LED schematic, we will connect the button to the Arduino in a specific manner for the circuit to work in the end. Follow along with the circuit schematic for a visual. First, we need to connect the Arduino's digital pin 2 (note that all digital pins except pin 0 or 1 can be used instead of digital pin 2) and a 10kOhm pullup resistor across a button's leg pair. This means that we need to connect pin 2 to one of the legs of the button, and the pullup resistor to the corresponding leg in the leg pair. The pullup resistor should then be connected to the GND pin of the Arduino. On the schematic, you can see this in how the pin 2 and the GND pin are connected on one end of the button. Next, we will connect the 5V pin from the Arduino to the other leg pair of the button. Note how the 5V pin is not connected to the digital pin and GND pin because the gate in the button is not closed (this occurs when the button is not pressed). When the gate closes, then the 5V is connected to the two other pins (this occurs when the button is pressed).
With the conceptual design done in the schematic, we should now design the circuit in TinkerCAD. We can do this by adding a button to the breadboard (buttons are usually placed across the bridge of the breadboard), and then connecting digital pin 2 to one of the button's leg pair. We will then connect a 10kOhm pullup resistor to the other side of the leg pair and then connect that resistor to the GND pin on the Arduino. Then, we are going to connect the 5V pin to the other leg pair, and our TinkerCAD model is complete. A picture of a correctly designed circuit in Fritzing and schematic are provided for you for reference; make sure you understand why their circuits look like that so that you can make your circuits with buttons and LEDs work.
Next, follow the TinkerCAD model with building the physical circuit on a breadboard. If you want to make sure you know which sides of the button are connected together, you can test continuity with a multimeter.
With the button added to the circuit, we can add the button into the Arduino code. In the code, we will set digital pin 2 an INPUT pin using pinMode(). This means that the Arduino will monitor digital pin two for any voltage coming into the Arduino. Conceptually, when the button is not pressed, the Arduino will read LOW voltage from digital pin 2 because digital pin 2 is connected to the GND pin (0V) of the Arduino via one of the button's leg pair. However, when the button is pressed, the button's two leg pairs become connected, connecting the 5V pin to digital pin 2 and the GND pin from the other leg pair. Because the 5V pin becomes connected to the digital pin 2, the Arduino will read a HIGH voltage at digital pin 2 because it is being input a 5V signal from the 5V pin. We will thus use this ability to detect whether or not the button is pressed and then change the state of the LED based on that.
Continuing on in the code, this code is going to differ from the previous code in that we are depending on an input from the button in order to turn on the light. The basic premise is a logic statement that checks if the button is pressed, then turns on the LED if it is. So, the code will incorporate an "if-else" statement that will turn on the LED when the Arduino senses a button press, and turns off the LED when the button is not pressed. The Arduino senses this by using the digitalRead() function to see if there is a HIGH or LOW voltage on digital pin 2, our input pin. Refer to the https://www.arduino.cc/reference/en/ website for more information on if-else statements or the digitalRead() function if necessary. While the code for this example is given to you in the images above, make sure that you understand how and why the code works.
Lastly, test and troubleshoot your circuit. If you wired your circuit correctly and used the code above, then your circuit should behave like the circuit in the video above. If your circuit behaves like it should, congratulations! You have completed a very useful Arduino circuit and you can move on to making your own circuit in the Electronics Tools Mastery Project. If not, double-check to see if your schematic is correct, then your TinkerCAD model, then your physical breadboard circuit, and finally your code.
Hopefully, this Instructable will prove useful in completing your Electronics Tools Mastery Project. Keep in mind the safety rules as you work on it and read the instructions to the assignment carefully. You may use the examples in this Instructable as references, but be sure to provide necessary documentation, such as attributing code. Good luck on your Electronics Tools Mastery Project and any future circuits that you will build!