Event booking for offline workshop on Arduino Alvik started. Seats: 16 only. Click here to Book Now!
- Arduino Alvik: How to write the blinking LED code?
- How to write code for alternate Blinking LEDs with different colors in Arduino Alvik?
- How to move Arduino Alvik forward and backward using simple code?
- How to give turns to Arduino Alvik using simple code?
- Achieving control and orientation with Arduino Alvik on rotating platform
- Simple black line follower code for Arduino Alvik
- Alvik: How to use alvik.drive() function in Arduino Alvik coding
- AlvikArduino Alvik Arduino Alvik robot is launched by Arduino on 14 May, 2024. Its the latest and the best educational robot now available at Vidyasagar Academy. Our expert faculties have designed well planned course to learn Arduino Alvik for any student from 4th standard up to engineering level. Read more about Arduino Alvik at official website or Arduino.: Detecting different colors & controlling the robot movements
- Alvik: Detecting Falling and Crashes (IMU) Corrected Code
If you are Arduino Alvik lover, then you must be wondering that how to use the color sensor in Alvik. This is because the there is no data or any code about color sensor, given on the official website of Arduino Alvik.
So I thought it will be helpful for you to write this article to guide you as to how to use the color sensor in Arduino Alvi.
Alvik api for Color Sensor
As per the official data, the Alvik api for using color sensor are as follows –
get_color
get_color(color_format: srt = 'rgb)
Returns the normalized color readout of the color sensor. The inputs are color_format: rgb or hsv
only. And the outputs are: r or h, g or s and b or v
.
hsv2label
hsv2label(h, s, v)
This returns the color label corresponding to the given normalized HSV color input. The inputs are –
- h: hue value
- s: saturation value
- v: brightness value
And the outputs are: color label: like “BLACK” or “GREEN”, if possible, otherwise return “UNDEFINED”.
But as per actual lab testing in Vidyasagar Academy Lab, we found that it can detect only following colors and that the detection of these colors is not in real time. That is if I use following code to detect and print the labels of the colors, by putting different color paper sheets, it does not return the color labels in real time properly. Sometimes it may show the same previous color even though the color paper sheet is changed below it.
The test code for printing color labels
from arduino_alvik import ArduinoAlvik
from time import sleep_ms
alvik = ArduinoAlvik()
alvik.begin()
while True:
# Detect the current color
current_color = alvik.get_color_label()
print(f"Current color detected: {current_color}")
sleep_ms(5000)
Output
🪫 5% Alvik is on
Current color detected: BLACK – No sheet placed
Current color detected: RED – Red color sheet placed
Current color detected: LIGHT BLUE – Light Blue color sheet placed
Current color detected: LIGHT GREEN – Light Green color sheet placed
Current color detected: YELLOW – Yellow color sheet placed
Current color detected: YELLOW – Pink color sheet placed, still showing yellow
Current color detected: YELLOW – Orange color sheet placed, still showing yellow
These are the limitations of Alvik color sensor. Broadly it can sense only 3 types of colors correctly: RED, GREEN or LIGHT GREEN, BLUE or LIGHT BLUE. Also sometimes it cannot detect white color properly – it mistakens WHITE color for YELLOW color.
Use of Alvik api of color sensor in coding?
The following tested code at Vidyasagar Academy Lab gives correct results for the color sensor of Alvik. This code detects RED color while moving forward. When it detects RED color, it stops, move backward and turn to avoid the RED color area. Try it for yourself –
from arduino_alvik import ArduinoAlvik
from time import sleep_ms
alvik = ArduinoAlvik()
alvik.begin()
while True:
color = alvik.get_color_label()
print(f'Detected Color: {color}')
if color == "RED": # danger zone detected
alvik.brake() # stop
sleep_ms(1000)
alvik.move(-10, 'cm') # come backward to avoid danger zone
alvik.brake()
sleep_ms(1000)
alvik.rotate(-90.0, 'deg') # turn, use either 90 or -90
sleep_ms(1000)
else:
alvik.move(10, 'cm') # go forward 10cm then stop for 1 second
alvik.brake()
sleep_ms(1000)
I have noticed here while working on it that when the surrounding light is rather dull, it mistakens RED for BLACK and WHITE or YELLOW and so on. So while using color sensor of Alvik, be careful that your Alvik must be placed in bright surrounding light.
Now using this code you can extend it as per your imagination. If you have any queries, please write your comment below this post.
Happy Alvik Coding…!
Quick Links
- Achieving control and orientation with Arduino Alvik on rotating platform
- Arduino Alvik: How to write the blinking LED code?
- How to write code for alternate Blinking LEDs with different colors in Arduino Alvik?
- How to move Arduino Alvik forward and backward using simple code?
- How to give turns to Arduino Alvik using simple code?
- Simple black line follower code for Arduino Alvik