Education

Education

Red Green Blue LEDs

Fun Activity!

Welcome to a fun, hands-on activity that turns numbers into blinking lights! In this project, you'll use your Yellow Tyro board and the five LEDs to see how computers talk using binary code. Here's how it works:

When you type a number between 0 and 31 into your computer, the Arduino converts that number into binary—a language made up of only 0s and 1s. Each LED represents one of these binary digits. If a digit is 1, the corresponding LED lights up; if it's 0, the LED stays off. It's like a magic trick where your number turns into a secret pattern of lights! This activity is a great way to learn about binary numbers, how computers store information, and to have fun experimenting with electronics. Enjoy bringing your numbers to life with blinking LEDs!

/*
                         JumpLabs®
      Follow us on Instagram: jumplabs.co
                 LinkedIn: JumpLabs
         "Bringing Families Closer!"

 This sketch reads a number (0-31) from the Serial Monitor,
 converts it to binary, and displays the binary value on 5 LEDs.
 Each LED corresponds to one bit of the 5-bit binary number.
*/

const int ledPins[5] = {3, 4, 5, 6, 7};  // LEDs connected to digital pins 3, 4, 5, 6, and 7

void setup() {
  Serial.begin(9600);   // Start serial communication at 9600 baud
  Serial.println("Enter a number between 0 and 31:");
  
  // Set each LED pin as an OUTPUT and turn them off initially
  for (int i = 0; i < 5; i++) {
    pinMode(ledPins[i], OUTPUT);
    digitalWrite(ledPins[i], LOW);
  }
}

void loop() {
  // Check if a number is available from the Serial Monitor
  if (Serial.available() > 0) {
    int number = Serial.parseInt(); // Read the entered number
    Serial.print("You entered: ");
    Serial.println(number);
    
    // Ensure the number is between 0 and 31 (5-bit limit)
    number = constrain(number, 0, 31);

    // Display the number in binary on the LEDs.
    // The least significant bit is shown on ledPins[0] (pin 3) and the most on ledPins[4] (pin 7)
    for (int i = 0; i < 5; i++) {
      int bitValue = bitRead(number, i); // Read the i-th bit of the number
      digitalWrite(ledPins[i], bitValue); // Turn the LED on (if 1) or off (if 0)
    }

    // Also print the binary representation to the Serial Monitor for confirmation.
    Serial.print("Binary: ");
    for (int i = 4; i >= 0; i--) {
      Serial.print(bitRead(number, i));
    }
    Serial.println();
    Serial.println("Enter another number between 0 and 31:");
  }
}

How It Works

  • Setup:
    The sketch starts by opening the Serial Monitor at 9600 baud and instructs you to enter a number between 0 and 31. It also sets pins 3, 4, 5, 6, and 7 as outputs for the LEDs.

  • Reading the Number:
    In the loop(), the Arduino checks if a number is available from the Serial Monitor. It reads that number and makes sure it is between 0 and 31.

  • Converting to Binary:
    Using the bitRead() function, the code checks each bit of the number (from the least significant bit to the most significant bit). Each bit value determines whether the corresponding LED is turned on (if 1) or off (if 0).

  • Displaying the Result:
    The binary representation is displayed both on the LEDs and printed to the Serial Monitor.

This fun activity lets you see numbers come to life as binary patterns on your LEDs—an exciting way to explore how computers use binary to represent information!

Socials

info.jumplabs@gmail.com

Call Us

+4550142069

+91 9910566229

Damgade 82, 6400 Sønderborg

© Jumplabs Inc. 2023

Reach out to Us

Socials

info.jumplabs@gmail.com

Call Us

+4550142069

+91 9910566229

Damgade 82, 6400 Sønderborg

© Jumplabs Inc. 2023

Reach out to Us

Socials

info.jumplabs@gmail.com

Call Us

+4550142069

+91 9910566229

Damgade 82, 6400 Sønderborg

© Jumplabs Inc. 2023

Reach out to Us