Punch Through
Published © MIT

Light Sensor

Curious how to modify the RGB LED based on how much light is shining? Find out by doing this project!

IntermediateFull instructions provided1,370
Light Sensor

Things used in this project

Hardware components

LightBlue Bean
Punch Through LightBlue Bean
×1

Software apps and online services

Punch Through Bean Loader
Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Code

Arduino Sketch for the LightSensor

Plain text
#define BLEVEL 255
#define MINLEVEL 50

void setup() {
  // initialize serial communication
  Serial.begin(57600);
  pinMode(0, OUTPUT);  // set D0 as output
  digitalWrite(0, LOW);  // turn it off initially
}

void loop() {
  digitalWrite(0, HIGH);
  Bean.sleep(10);
  int sensorValue;
  sensorValue = analogRead(A1);
  int r, g, b;
  if (sensorValue > MINLEVEL && sensorValue < BLEVEL + 1) {
    g = (sensorValue - MINLEVEL)/2;
    b = 0;
    } else if (sensorValue > BLEVEL) {
        g = sensorValue - MINLEVEL;
        if (g > 255) {
          g = 255;
        }
      b = sensorValue-BLEVEL;
      if (b > 255) {
        b = 255;
      }
    } else {
      g = 0;
      b = 0;
    }

  Bean.setLed(0, g, b);
  Bean.sleep(100);
}

Credits

Punch Through

Punch Through

16 projects • 41 followers
We’ve been building connected products since 2009. Our diverse team has expertise in every layer from hardware to software to web.

Comments