Pulse Width Modulation
Try reloading this page, or reviewing your browser settings
This segment is the viewer’s first introduction to using an LED with Arduino. Step-by-step instructions are included for building a simple circuit with an LED, opening up code in the Arduino IDE and using the serial monitor to view realtime data from the Arduino.
- hello world
About this video
- Liz Clark
- First online
- 09 April 2019
- Online ISBN
- Copyright information
- © Liz Clark 2019
In this section, we’ll go over how to blink an LED, also known as the Hello World of hardware. To follow along you’ll need an Arduino Uno, at least one LED, at least one to 20 ohm or similar value resistor, some jumper wires, and a breadboard. Let’s get started. We’ll begin by building the circuit for one LED. First, insert an LED into a breadboard as shown here, followed by a resistor connected to the anode, or positive lead. Then take a piece of jumper wire and connect it from the resistor to digital pin 13 on the Arduino Uno.
The final connection will be with another piece of jumper wire, this time connecting the LED’s cathode, or negative lead, to one of the ground pins on the Arduino Uno. After your circuit is all set, we can move to the Arduino IDE to look at the code for blinking an LED. The Arduino IDE is a piece of open source software that allows you to write an upload code for Arduino boards. The Arduino IDE also has an assortment of example programs included for more common tasks.
These can be accessed by going to File, Examples, where you’ll then see a long list of options. To blink our LED, we’re going to utilize the built in blink example. To access it, go the basic section under Examples and select Blink. Now that we have Blink open, we can see that it’s a very simple program with a quick, one line setup, followed by a loop. In the setup, you can see the LED built in is being called to be an output. This is a special option in the Arduino IDE that allows you to quickly utilize the onboard LED.
This LED is connected to pin 13, so our LED on our breadboard will be receiving the same data from the code. Setting up pin 13 as an output means that that PIN will be receiving data from the program once it begins running. In this case, whether or not to have the LED blink. The actual blinking is taking place in the loop, the portion of the code that will run over and over again until the program is stopped. This is achieved with just four lines of code.
The first sends a high signal to pin 13, which essentially means turning the pin on digitally, since digital signals are either on or off, ones or zeros. Sending a high signal pin 13 will allow the LED to turn on, since the LED will be receiving voltage when the pin is in that state. This is followed by a delay of 1,000 milliseconds, or one second, which means that the LED will then remain on for one second. Then pin 13 is set to low or off, which will in turn turn our LED off.
The low signal is followed by another delay of one second which, of course, means that the LED will be turned off for one second. This pattern of turning the LED on and off, or blinking, will continue infinitely. Let’s compile the code and upload to the Arduino Uno to see it in action.
What could be better than blinking an LED, though? Well, blinking multiple LEDs, of course. Using the same code, we can achieve this by adding a few LEDs and resistors to our breadboard. First, let’s rearrange the wires that are connected to pin 13 and ground on the Arduino. Plug pin 13’s wire to the positive rail on your breadboard, and ground’s wire to the ground rail. This will make it easier to integrate more LEDs. Now connect the original LED’s cathode to the ground rail either directly, or with another piece of wire, and the anode, via the resistor, to the positive rail.
After the original LED is in its new position, repeat those steps with as many LEDs as you’d like. To a point, of course. After adding more than 10, you may have to consider things like voltage drop, and other power considerations. But you shouldn’t run into any issues with three or four. Once your new circuit is all set, compile the blink program again and upload it to the Arduino. You should see all of your LEDs blinking in unison.
Let’s say that you wanted to test your code to make sure it was doing exactly what you thought it was doing. Although this blink program is fairly simple and straightforward, it’s good practice to log the status of the code for testing purposes. For blink, we’re going to use the Arduino IDE’s built in serial monitor to print pin 13 status so that we can compare it with what the LCD is doing in real life. To do this, we’ll first need to open up serial communication for the Arduino by entering serial.begin9600 under the setup portion of the code.
The 9600 is the BOD rate that we’ll be using to communicate over serial. There are a few different BOD rates the Arduino can use, but 9600 is one of the most common for printing data like this to a serial monitor. Then in the loop under the digitalWrite HIGH line, we’re going to put serial.println, LED on, making sure to put the text in quotations so that the Arduino knows it’s a string. The ln after print stands for line, and allows your text to be printed to a new line, so that your data will be nice and neat in the serial monitor.
Then under digitalWrite LOW, we’re going to insert serial.println, LED off. Compile the code and upload to your Arduino. Then in the Arduino IDE, go to Tools, Serial Monitor. This will open a console, and you should see LED on and LED off alternating in time with your blinking LED.