Using an Arduino to control a blinkM with a photocell

As mentioned in my previous post Arduino Night Light using a Photocell, I was awaiting the arrival of a BlinkM via snail mail. Days later the good ole mail man handed me the package which contained my BlinkM(s) and warranted this post. The night light prototype that we will be building basically, changes the color of the BlinkM based off the photocells reading. For simplicity and for testing purposes, it only displays two states, night and day, and represents the states with the color red (day) and blue(night).

Make sure you have all the components needed to recreate this prototype:

  • Arduino Duemilanove
  • Breadboard
  • 10 K resistor
  • BlinkM
  • Photocell
  • Breadboard wires
  • Lets start building the prototype.

    The first thing I always do when prototyping, is connect the Arduino to a power source either via the USB or an external power supply and then I proceed to wire up the bread board to the Arduino power.

    I advise you to read datasheets for the components that you will be working with, be aware that you may need to disconnect all power before connecting or disconnecting components to a circuit.

    I simply take a Red wire from the Arduino 5v(+) and connect it to the red rail on the breadboard. Then take a Black wire and connect it to the GND of the Arduino and connect it to the blue rail on the Breadboard. We now have power!

    The next step is to connect the Photocell to the breadboard and wire it up to the Arduino.
    Add the Photocell to the board. Connect a Red wire from the red rail on the board to one of the leads of the Photocell.

    Now, lets add the 10k resistor. Connect one end of the resistor to the other lead of the Photocell.

    Connect a wire from the same Photocell lead that the resistor resides on to Analog 0 on the Arduino. Then ground the resistor by adding a black wire from the blue rail on the breadboard to the other end of the resistor, like so.

    You have successfully created a basic Photocell circuit. Now, for a sanity check copy the code below, open up the Arduino Ide and create a new photocellTest.pde file, compile, upload to your Arduino and view the output of the Photocell in the serial monitor.
    This example shows the output of an analogRead() of a Photocell.
    By M.Gonzalez
    The example code is in the public domain

    int photocellPin = 0;// Photocell connected to analog pin 0
    int photocellVal = 0; // define photocell variable

    void setup() {
      pinMode(photocellPin, INPUT);
    void loop() {
      photocellVal = analogRead(photocellPin);// read the analog from photocell
      Serial.println( photocellVal);    // print to screen


    If the Photocell circuit is correct you should be viewing numbers printed in your serial monitor. Place your hand over the resistor to simulate darkness and witness the numbers change. We will be using the Photocell output as a switch to change the color of the BlinkM for red to blue. Grab your BlinkM and lets get familiar with it.

    The BlinkM is a networkable and programmable full-color RGB LED. It has a small AVR micro-controller on board to allow a user to digitally control the LED over a simple I2C interface. The Arduino will communicate with the BlinkM, using the Wire library so we don’t have to worry about the details of I2C. If you haven’t already checked out the BlinkM support page please do so, it has a bunch of perks you should be aware of. Take a look at the print on the board itself. You will notice PWR and I2C printed directly on the board, take mental note because that’s what we will be interfacing with shortly. BlinkM needs two wires for power and two for data. We will be connecting to the Arduino analog pins 4 & 5 which double as the I2C data signal (“SDA”) and clock signal (“SCL”), respectively. Place the BlinkM onto your breadboard.

    Power up the BlinkM by connecting a Red wire from the red rail on the breadboard to the positive (+) symbol printed on the BlinkM. Connect a Black wire from the blue rail on the breadboard to the ground (-) symbol printed on the BlinkM. If the power connections are correct, you will notice a pre-programmed color sequence which comes shipped as a default with the BlinkM.

    Proceed by hooking up the BlinkM to the Arduino. Grab a breadboard wire and connect it to analog 4 of the Arduino. Connect the other end of the wire to the ‘d’ (I2C SDA) symbol printed on the BlinkM. Grab another breadboard wire and connect it to analog 5 of the Arduino. Connect the other end of the wire to the ‘c’ (I2C SCL) symbol printed on the BlinkM.

    The prototype circuit is complete. Now, lets focus on the code. I wrote a simple sketch which utilizes Tod E. Kurt’s BlinkM Arduino library to fade the colors of the LED from red to blue. You will need the BlinkM_funcs.h library which you can obtain from the BlinkM example files. Better yet, I have made it available with my source code down below. Either way, you will have to place the BlinkM_funcs.h in the same directory as the Arduino sketch and “#include” it at the top to import all the functions. The BlinkM has many features, and I would advise you to download the datasheet if your interested, anyhow, here’s my simple sketch.

    Code under (cc) by Manuel Gonzalez,
    Analog pin 0 photocell
    Analog pins 4 (SDA),5(SCL) I2C communication with BlinkM

    #include <Wire.h>
    #include "BlinkM_funcs.h"

    int blinkm_addr = 9;//default address to blinkM
    byte r,g,b;

    int photocellPin = 0;// Photocell connected to analog pin 0
    int photocellVal = 0; // define photocell variable
    int minLight = 100;//min light threshold
    int maxLight = 100;//max light threshold
    int ledState = 0; //state of the led 0 = dark, 1 = light

    void setup() {
      pinMode(photocellPin, INPUT);

      BlinkM_stopScript(blinkm_addr);//stop the BlinkM
      BlinkM_setFadeSpeed(blinkm_addr, 1);//set up fade param for a smooth fade

    void loop() {
      photocellVal = analogRead(photocellPin);//get photocells value

    //conditional which determines if its dark or not
      if (photocellVal < minLight and ledState == 1){
        else if (photocellVal > maxLight and ledState == 0){


    void displayColor(int val)

        case 0:
          //0;34;102 Royal Blue 5
          r = 0;
          g = 34;
          b = 102;
          ledState = 0;
        case 1:
          //255;0;0 RED
          r = 255;
          g = 0;
          b = 0;
          ledState = 1;
        BlinkM_fadeToRGB(blinkm_addr, r,  g,  b);//call which fades to desired color

    You will notice in the displayColor() method I’m setting R,G,B values to change the color of the blinkM. BlinkM supports two different color models: RGB and HSB. RGB is the color model most people are familiar with so that’s why I used it. Here’s an online resource I use for RGB mapping. And there you have it, a simple prototype using Arduino a photocell and a BlinkM to create an interesting nightlight or ambient light.

    Download Source:
    [download id=”14″]

    Be Sociable, Share!
    Coding Color | 2009 All Rights Reserved.