Magicbit
Published

Simple DIY Colour Sensor From Magicbit

In this tutorial we will learn about how to make a simple color sensor using Magicbit with Arduino

BeginnerProtip449
Simple DIY Colour Sensor From Magicbit

Things used in this project

Hardware components

Magicbit
×1
USB-A to Micro-USB Cable
USB-A to Micro-USB Cable
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

Arduino code of color sensor

Arduino
study further how this works
#include <Adafruit_NeoPixel.h>
#define LED_PIN  33
#define LED_COUNT 1
Adafruit_NeoPixel LED(LED_COUNT,LED_PIN, NEO_RGB + NEO_KHZ800);
#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() {
  LED.begin();
  LED.show();
  pinMode(LDR,INPUT);
  pinMode(Button,INPUT);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.display();
  delay(1000);
  display.clearDisplay();
  Serial.begin(9600);
}

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

}
void Display(String commond,int size) { // display data
  display.clearDisplay();

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

}

Credits

Magicbit

Magicbit

57 projects • 34 followers
Magicbit is an integrated development platform based on ESP32 for learning, prototyping, coding, electronics, robotics, IoT and more.

Comments