Learning Outcome
In this chapter, we learn to
program EDU PICO to light up RGB LEDs in different colours.
read colour data with colour sensor.
setup lists to store multiple items.
Introduction: RGB LEDs and Colour Sensor
Activities: Build a colour detection game (Colour Blindness Tester).
Challenge 1: Program EDU PICO to avoid repeating 2 similar colour during gameplay.
Challenge 2: Program a colour hint indicator by lighting up a single RGB LEDs.
Introduction to RGB LEDs
In this introduction, you will learn how to control the RGB LEDs module to create dazzling displays of colour and patterns! Once you're done with the sample code, make sure to try building something with your own way.
> Libraries <
-> Libraries: board, time, neopixel.
-> RGB LEDs Configuration: num_pixels = 5, pixel_pin = GP14.
< Expected Output >
Upon completion of the activity, you should be able to:
-> The program will turn all LEDs on (blue color) and off with a 1 second interval.
Introduction to Colour Sensor
In the previous chapter, we explored the APDS9960 sensor, which was initially used to detect gestures. However, you might be surprised to learn that this sensor is not limited to just detecting gestures. If can also be used to identify and distinguish different colours!
> Libraries <
-> Libraries: board, time, busio, neopixel, adafruit_apds9960.
-> RGB LEDs Configuration: num_pixels = 5, pixel_pin = GP14.
-> APDS9960 I2C Pins Configuration: SCL = GP5, SDA = GP4.
-> Input:
Hold the colour cards above the colour sensor.
< Expected Output >
Upon completion of the activity, you should be able to:
-> The program will light up the RGB LEDs in white colour at 20% brightness.
-> The color sensor reads and display the amount of red (r), green (g), blue (b), and clear (c) light values with a 1 second interval.
Mini Activity
Modify the code to be able to compare and identify between Red, Green, or Blue by using the if..elif..else statement. While you’re at it, make use of the AND logical operator to determine the most prominent colour in the data.
> Libraries <
-> Libraries: board, time, busio, neopixel, adafruit_apds9960.
-> RGB LEDs Configuration: num_pixels = 5, pixel_pin = GP14.
-> APDS9960 I2C Pins Configuration: SCL = GP5, SDA = GP4.
-> Input:
Hold the colour cards above the colour sensor.
< Expected Output >
Upon completion of the activity, you should be able to:
-> The program will display text on the shell console which color is detected after the color identification.
Colour Detection Game
Colour Blindness Tester
Did you know that approximately 300 million people worldwide have colour vision deficiency, also known as colour blindness? This condition can make it difficult to distinguish between colours, affecting a person's education, academic performance, and even career choices. In this project, we will learn how to build a basic colour detection game by integrating colour sensor, RGB LEDs, buzzer, button and OLED display.
> Libraries <
-> Libraries: board, digitalio, time, simpleio, neopixel, busio, random, adafruit_ssd1306, adafruit_apds9960, font5x8.bin.
-> OLED & APDS9960 I2C Pins Configuration: SCL = GP5 and SDA = GP4.
-> Audio / Buzzer Configuration: Buzzer to GP21.
-> RGB LEDs Configuration: num_pixel = 5, pixel_pin = GP14.
-> Buttons Configuration: button_start (Yellow) to GP0 as digital input.
-> Input:
Press Button A to start the game with a 5 second countdown.
The player must quickly place the correct colour card above the colour sensor once the RGB LEDs white light turns on.
< Expected Output >
Upon completion of the activity, you should be able to:
-> Display text of a random color on the OLED
-> The buzzer will play an exciting tone with a "Well Done!" message printed on the OLED, if the player gets the correct colour.
-> The buzzer would beep 3 times and the OLED would proceed to display text "Try Again", if the player got the wrong color.
-> The RGB LEDs will turn off after the countdown of 5 second ends.
Challenges
#1 - Improve Gaming Experience
It can frustating as a player to receive three identical colours in a row. Unfortunately, using the random function means there's still a chance that will happen.
We can include a "check-code" before generating a random colour to eliminate the possibility of repeating the same colours. While we are at it, let's build a function to generate the random, this will help us keep the code neat too.
> Libraries <
-> Libraries: board, digitalio, time, simpleio, neopixel, busio, random, adafruit_ssd1306, adafruit_apds9960, font5x8.bin.
-> OLED & APDS9960 I2C Pins Configuration: SCL = GP5 and SDA = GP4.
-> Audio / Buzzer Configuration: Buzzer to GP21.
-> RGB LEDs Configuration: num_pixel = 5, pixel_pin = GP14.
-> Buttons Configuration: button_start (Yellow) to GP0 as digital input.
< Expected Output >
Upon completion of the activity, you should be able to:
-> Build a function in the program to generate the random colour to eliminate the possibility of repeating the same colours.
#2 - Colour Hint Indicator
You probably wonder what else can be done for our fellow gamers. For one, we can improve the game by providing hints to the player. Here's how it works, the flow of the game will still be the same, but instead of lighting up all RGB LEDs in white, first RGB LEDs ID:0 will light up according to the same random colour. This will allow the user to colour match and increase their chance of getting the right answer too. Give it a try!
> Libraries <
-> Libraries: board, digitalio, time, simpleio, neopixel, busio, random, adafruit_ssd1306, adafruit_apds9960, font5x8.bin.
-> OLED & APDS9960 I2C Pins Configuration: SCL = GP5 and SDA = GP4.
-> Audio / Buzzer Configuration: Buzzer to GP21.
-> RGB LEDs Configuration: num_pixel = 5, pixel_pin = GP14.
-> Buttons Configuration: button_start (Yellow) to GP0 as digital input.
< Expected Output >
Upon completion of the activity, you should be able to:
-> Build a function in the program by lighting up RGB LEDs ID = 0 to give player a hint and increase their chance of getting the right colour.
Bravo on Completing Chapter 4! Onto the Next Chapter!