Mid-Senior Engineers: Reinvent your career with Robotics, earning $100k-$200k+ in the next 90 days. Learn More

Random LEDs with Arduino Uno

Liz Miller Learn Robotics

About the Author, Liz Miller, Founder/CEO @ Learn Robotics

Liz graduated with a degree in Robotics Engineering from Worcester Polytechnic Institute and researched drones at UPenn's GRASP Lab. Liz is a former Raytheon Engineer, where she managed major $MM automation projects worldwide. Today, she's the driving force behind Learn Robotics, offering the Robotics Career Blueprint for Engineering Professionals and beginner courses through the Online Robotics Class. Liz is a third-generation entrepreneur who is all about the application of innovation in robotics, automation, and AI. Follow Liz on LinkedIn and Facebook.

Disclosure: Some of the links below are affiliate links. This means that, at zero cost to you, Learn Robotics will earn an affiliate commission if you click through the link and finalize a purchase. Learn Robotics is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a way for websites to earn advertising revenues by advertising and linking to Amazon.com.

Join our Private Discord Server, MakeRobots! Sign Up

Blinking LED’s is a staple example of beginner Arduino programming. In this tutorial, we’re going to step it up a notch and use both arrays and the random() method to determine which LED to turn on and off for a duration.

Materials for Random LED project

Here’s a kit you can use which has all the components.

 

Amaze your friends with this randomized LED generator

This project is not only fun, but it’s rather straightforward. We will wire the LED to a digital input pin on our Arduino. Then, we’ll send a signal to the pin we want to trigger. This will serve as the “random” LED.

Grab your materials, and let’s wire up the circuit.

Define the process before writing any code

We’re going to start by mapping out the process of how this device should work.

First, we want to determine which LED should turn on. We will use the random() method from the Arduino Library.

Next, we want to determine which color has been selected. We can use an array of Strings for each color name to identify the selected LED. We can initialize the Serial Monitor and then print out the color once it’s been selected.

Finally, we’ll turn the chosen LED on and off using the same strategy as the Blink example sketch, digitalWrite(...) and delay(...).

For clarity, I’m going to wrap this all up into a separate method and then call the method in loop(). You’re more than welcome to include the code in loop(), but I think it looks cleaner writing a separate method.

Translate your process into Arduino code

Now that we have ourselves a roadmap, we’ll open up the Arduino IDE, and translate the process into code.

Start by mapping your LED’s

I always recommend creating global variables for all Inputs and Outputs connected to your Arduino board. Then when you want to use that sensor or device, it’s already defined within your code!

/*
   Random LED Generator
   Last Revision: 6/7/18
   Written by Learn Robotics
   www.www.learnrobotics.org
   --------------------------
   Pin Wiring:
   ===========
   OUTPUTS
    - BUILT-IN LED: 13
    - RED LED: 12
    - YELLOW LED: 11 | PWM CAPABLE
    - GREEN LED: 10 | PWM CAPABLE
    - WHITE LED: 9 | PWM CAPABLE
*/

// OUTPUTS
int red_led = 12;
int yellow_led = 11;
int green_led = 10;
int white_led = 9;

/*
Create an array (size 4) to store color names
led_colors[0] = "white"
led_color[1] = "green"
led_color[2] = "yellow"
led_color[3] = "red"
*/
String led_colors[4] = {"white", "green", "yellow", "red"};

void setup() {
  // put your setup code here, to run once:

  //configure outputs
  pinMode(red_led, OUTPUT);
  pinMode(yellow_led, OUTPUT);
  pinMode(green_led, OUTPUT);
  pinMode(white_led, OUTPUT);

  Serial.begin(9600); //enable the serial monitor
}

Also, call pinMode(...) for each LED. They’ll be OUTPUTS in this case, since we are sending data from the Arduino to the outside world. Then, initialize the Serial Monitor, by calling

Serial.begin(9600);

We’ll use this to print out the color on the Serial Monitor.

The color of the LED will be stored into an array of Strings.

String led_colors[4] = {"white", "green", "yellow", "red"};

An array stores values in an indexed list. That means, we can call the array variable, led_colors using an index number 0-3. In computer programming, you start counting indexes at 0. Therefore, white is 0, green is 1, yellow is 2, and red is 3. So, if you want to get the value green, you’d reference led_colors[1]. We’ll expand on this concept to print out the color name once we obtain the randomly generated LED pin.

Select a Random LED and identify the color from the array

The random number we’ll select will be between 9 and 13 because we have LEDs plugged into these pins. Then, we’ll map the selected number 9-13 to the range 0-3, which will form the index of the color_map array. Next, we can print out the color using the mapped index, our led_color array, and the Serial.print(...) command. Finally, we turn the selected LED pin HIGH (on), delay for 1/2 second, then LOW (off), for 1/2 a second.

This is the code we obtain once we piece it all together.

/*
   Select a random LED to turn on
   Print out the name of the color
   in the Serial Monitor
*/
void random_led() {
  // pick a pin number 9-13 because we have LEDs on those pins...
  int random_led = random(9, 13);
  
  // generate our map of the random LED to the map of our array
  int color_map = map(random_led, 9, 12, 0, 3);
  Serial.print("Color is... ");
  // Use the random array number to print out the color
  Serial.println(led_colors[color_map]);

  //turn the LED on, then off for 1/2 second
  digitalWrite(random_led, HIGH);
  delay(500);
  digitalWrite(random_led, LOW);
  delay(500);
}

Call the random_led() method in loop(). Then upload the code to your Arduino & test it out!

https://www.instagram.com/p/BjS_6qmgVS4/?taken-by=learnrobotics

Snag our Arduino Project eBook!

If you liked this tutorial, you’re going to want to check out our Arduino Projects Workbook. Build over 20+ hours worth of Arduino Prototypes that are built with Arduino and common electronics. You won’t want to miss this!

Have question? Drop it in the comment section!

And don’t forget to tag us in your Random LED Dance Party @learnrobotics on Facebook & Instagram!

Experienced Engineer (Mechanical, Electrical, Computer, Software): If I offered to help you upgrade your engineering career to robotics, earning $100k-$200k+ in the next 90 days, would you take me up on that offer? Click here for details.
Liz Miller Learn Robotics

🚀 Pre-Launch: Become a "MakeR" with MakeRobots!

Hey Reader, 👋

Liz Miller, Founder/CEO, here with some Exciting News!

Learn Robotics just acquired MakeRobots™, an Online Robotics Community, and are prepping its Official Learn Robotics Debut in Late 2023.

MakeRobots™ is your one-stop-shop for learning, gaining coding, electronics, and robotics skills, connecting, and building robots for one low monthly membership!

Join MakeRobots™ at our Special Pre-launch Rate!
🤖 Access our Private Community & Robotics Courses
💬 Network, Collaborate, Connect with Other Makers
🔓 Only $5.99/month – locked in for life
⏱️ Pre-launch deal is Limited to the first 1,000 subscribers

This is a perfect opportunity for you to get into the fastest growing robotics community on the internet, at our ground-level, pre-launch membership rates.

👇 Click the button below to Claim your Pre-launch Membership and become a MakeR in the MakeRobots Community, today!

Learn Robotics Botly Favicon

MORE LEARN ROBOTICS ARTICLES

Coursera Plus is a new subscription service for online courses
Liz Miller

What is Coursera Plus?

Coursera rolled out a subscription service called “Coursera Plus.” Rather than pay per course, the subscription gives you access to 2,000+ courses and Specializations. Is Coursera Plus worth the subscription fee? Find out in this article.

Read More »