Simple DIY Color Sensor From Magicbit
Simple DIY Colour Sensor
Arduino Project

Simple DIY Colour Sensor From Magicbit

Make a simple color sensor using Magicbit with Arduino

Components Required


Hi guys, at some times you have to use color sensors for some purposes. But may be you don’t know how they are works. So In this tutorial you will learn about how to make a simple DIY Colour Sensor using Magicbit with Arduino. Lets get start.

Simple DIY Colour Sensor Pic 1

Theory and Methodology

In this project we expect to teach you to build color sensor which can detect red, green and blue colors. This is very basic example. So how to do this. For this purpose we use RGB module of the Magicbit and inbuilt LDR. First of all you have to learn about some theory. that is about light reflection amount. Now I am asking question from you. what colored surface which most reflect red color light ?also what surfaces which mostly reflect Green and Blue lights. Think little bit. But the answer is simple. The Red colored surface mostly reflect red color light. Also green and blue surfaces will reflect green and blue lights. So in this project we use that theory. To recognize color we emits red, green and blue lights one by one. At every time we measure reflection amount by using LDR value.if some light will give most reflection than other two lights, then that surface should be mostly reflected colored surface.

Hardware setup

This very simple. Plug your RGB module into upper right port of Magicbit. This module have WS2812B Neopixel LED. This LED have 4 pins. Two for power and two for Data in and out. Because we use one led we only need power pins and data in pin. If you don’t have that module you can buy and Neopixel module. If you bought that kind of module you have to connect power pins and data in pin to Magicbit. That is very easy. connect VCC and GND of the Magicbit to power pins of RGB module and D33 pin to data pin.

Simple DIY Colour Sensor Pic 2

Software setup

The most part done by the programming. We use Arduino IDE to program our Magicbit. In the code we use couple of libraries. They are Adafruit Neopixel library for control Neopixel LED and Adafruit OLED library for handle OLED. In the setup we configure our inputs and outputs. also configure the inbuilt OLED display on Magicbit. In the loop we check the left hand push button is pressed or not of the Magicbit. If it pressed, the input signal is 0. Because it is already pulled up by the board. If it pressed then we do color checking. If not then the screen will display “no color” statement. when button is pressed then automatically turn on red, green and blue lights one by one and store the reflection amount of the colors into three variables. Next we compared those values and select maximum value color to display as the output color.

So connect micro USB cable to Magicbit and select board type and com ports correctly. Now upload the code. Then it’s time to test our sensor. To test that, keep red, green or blue surfaced paper or sheet top on the LDR and RGB module and press left push button. Then OLED display will show the what is the color of the surface. If that wrong the reason is some color have high light intensity. As a example in every green surface the output is red then you have to reduced red light brightness from some amount. Because the red light have very high brightness in that case. So it have have high reflection. if you don’t know how to how to control brightness, then refer tutorial in link below.

In this link you can find how to control that RGB module from Magicbit. And also you ca find how to work with LDR and push button using Magicbit. Read that document and study further how to improve color sensor. Because this is very basic example about how color sensors are working. Most type of color sensors work in this way. So try to improve this by removing the ambient light noise and other noises.


#include <Adafruit_NeoPixel.h>
#define LED_PIN  33
#define LED_COUNT 1
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4

Adafruit_SSD1306 display(128, 64);
#define LDR 36
#define Button 35
int R_value,G_value,B_value;

void setup() {
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);

void loop() {
if(digitalRead(Button)==0){// if button is pressed
  LED.setPixelColor(0, LED.Color(0,50,0));  //on redcolour;
  R_value=analogRead(LDR);//get red mount
  LED.setPixelColor(0, LED.Color(150,0,0));//on greencolour;
  G_value=analogRead(LDR);//get green mount
   LED.setPixelColor(0, LED.Color(0,0,255));  //on bluecolour;
  B_value=analogRead(LDR);//get blue mount
  if (R_value>G_value && R_value>B_value){//red is most reflected
  else if (G_value>R_value && G_value>B_value){//green is most reflected
  else if (B_value>R_value && B_value>G_value){//blue is most reflected
  Serial.print(" GREEN=");
  Serial.print(" BLUE=");
  LED.setPixelColor(0, LED.Color(0,0,0)); //off RGB;
  Display("NO COLOUR",2);

void Display(String commond,int size) { // display data

  display.setTextSize(size);             // Normal 1:1 pixel scale
  display.setTextColor(WHITE);        // Draw white text
  display.setCursor(0, 20);            // Start at top-left corner


If you need help or couldn’t understand a step be sure to check out our youtube video by clicking here: Youtube Video

Related Posts
Leave a Reply