Arduino Project #1. How to Control an Output

This project will teach you how to control an output with an Arduino program. This is like the “Hello, World!” program that used to be the first program we learn with any programming language. For this project, we will create a program that does give us a visible output.

Components and Materials:

  • Arduino Board
  • LED
  • Jumper Wires
  • Breadboard

Circuit

As shown in the picture, we will connect Arduino pin 13 to the LED. The LED will be connected in series to a resistor and the other terminal of the resistor will be connected to ground.

The Sketch

The sketch above is an example code from the Arduino IDE that can be opened thru File > Examples > 01.Basics > Blink. The comments were removed for readability.

setup()

The setup() method contains a single line at line 2 that says:

pinMode(13, OUTPUT);

As the name implies, the function pinMode() sets the mode of the pin being used. There are two parameters that can be set for pinMode(): PIN number and mode. For this sketch, pin 13 is being used. The mode can either be INPUT or OUTPUT. For the blink sketch, pin 13 is being used as an output pin.

loop()

Let us look at the code inside the loop() method.

digitalWrite(13, HIGH);

The function digitalWrite has two parameters: PIN number and state. The state can either be HIGH or LOW. The line above means: set the state of Arduino pin 13 to HIGH, therefore turning our LED on.

delay(1000);

The delay function requires only one parameter which is the time in milliseconds. The line above means to pause the program for 1000 milliseconds or 1 second. Since the LED is turned on prior to this line, the LED will stay on for 1 second.

The code in the loop() continuously turns the LED ON for 1 second and OFF for 1 second. This will go on indefinitely as long as the Arduino is powered on.

Have you tried this program? Tell us if you have questions in the comments section below.

Leave a Reply

Your email address will not be published. Required fields are marked *