Basic LED Animations for Beginners (Arduino)

Pages
Contributors: Brandon J. Williams
Favorited Favorite 9

Simple ON/OFF with GPIO

It’s simple enough to just touch LED pins to a coin-cell, even add a little tape and you have a super simple flashlight. What about control, what about the pizzazz? Just because LEDs only allow current in one direction doesn’t mean you can only go one direction. We’re going to put together some simple components and play around with what microcontrollers can do to for our lighting needs.

Let’s first talk about GPIOs. These are General Purpose Input and Outputs, meant simply for input and output. This means that depending on how we set it up, that pin will receive a voltage signal or put out a voltage signal. On the RedBoard, our “On” (or output) voltage is a super useful 5 Volts. We make the ATMEGA IC do this by writing digitalWrite(PIN_NUMBER, HIGH); in our Arduino code. However, you'll notice that won't really work if you just copy paste that into the loop section. We have to declare what we want that pin to be. So we make a variable to easily reference our pin number: PIN_NUMBER. For the simplicity, we'll use the defualt pin 13 on the RedBoard Qwiic. Then up in setup() function, we need to write pinMode(PIN_NUMBER, OUTPUT);. What this means is that our IC is dedicating that pin to be an output. So when we write that digital high, the IC knows exactly what to do. If you hooked up an multimeter, or oscilloscope, that pin would be at 5V relative the GND (0V) until you declare it LOW with digitalWrite(PIN_NUMBER, LOW);. In a nutshell, that's a blink, off to on and then back to off. We can add time delays for different effects.

language:c
//This example turn a GPIO pin ON and OFF with a delay.

const int PIN_NUMBER = 13; //Replace 'PIN_NUMBER' with a physical pin, we'll use the built-in LED on pin 13 on the RedBoard Qwiic

void setup() {
  pinMode(PIN_NUMBER, OUTPUT);    //set digital pin as output
}

void loop() {

  digitalWrite(PIN_NUMBER, HIGH); //LED connected to pin 13 comes ON
  delay(5000);                    //delay(Milliseconds), so '5000' is 5 seconds
  digitalWrite(PIN_NUMBER, LOW);  //LED connected to pin 13 goes OFF
  delay(5000);                    //delay(Milliseconds), so '5000' is 5 seconds

}

"Ok, What Does this Mean for Me?"

This demonstrates that these "auxillary pins" can be for input, or in our case for output. There are so many options after we make this step. From this point forward, we're going to explore what some fun arrangements, effects, and practical uses for LEDs can be.

I may be hammering home too much about pins on the microcontroller. They warrant their own slew of tutorials, but we're not here for those. I'm pushing them because they will be the web of control for our multiple LEDs.

The big picture is that at some spot in our circuit, there is a point of voltage. If this point of voltage is high enough (and not too high, so we don't blow the LED) then we can illuminate our LED. The output of a GPIO pin is a power source for our needs. One could think of them as fancy switches in a room full of switches and bulbs.