Sensing color with the ADJD-S311 + Arduino

Tuesday, January 31 st , 2012

A year ago we had an article called Sensing color with the ADJD-S371. Well the ADJD-S371 is long gone, and its replacement is the ADJD-S311 (breakout board available from sparkfun). It is basically the same all around, so without shame, I will be copying a lot of that article, because… Why reinvent the wheel?

A few things you should know about the ADJD-S311 before we dive in too deep: I have never been able to get perfect color sampling from this guy without limiting the colors it would detect to just 6, and accurately reproducing color on an LED is not as simple as one would hope. The color reading from the sensor could be spot on, but the reproduced color on the LED may be way off. (you can blame your eyes and the LEDs)

Hooking it up

The ADJD-S311 is an I2C device (I2C is a 2-wire serial connection), so we just need to connect the SDA (Data) and SCL (Clock) lines to your Arduino for communication. On your Arduino (everything but the mega) SDA is on analog pin 4, and SCL is on analog pin 5. On an arduino mega, SDA is digital 20, and SCL is digital 21. (The Arduino Leonardo will also be different). Other than these 2 lines, we just need to connect power(3.3v), ground, and the onboard LED to digital 2 (you can change that in code if you want).

The ADJD-S311 has 4 sensors built into it to detect Red, Green, Blue, and Clear. It reports back an individual reading from each sensor. The white, or clear, sensor is mainly for sensing brightness.

The ADJD-S311 is designed to sense reflected light using the onboard LED, but I have found that by turning the onboard LED off, it works even better for sensing projected light (like from your monitor or projector). If you are interested in using the sensor for sensing reflected colors, you want the sensor to be about 1-2mm from the subject so the onboard LED can bounce off the material and back to the sensor.

Code

The code for this guy is long and obnoxious, so we made a library out of it. This code is based on the sparkfun code, that was based on the old bildr code, that was based on the code by Marcus over at Interactive Matter. I love open source!

This library is really simple, you can call calibrate to calibrate to white (make sure you have a white objet or light source in front of it when you do this), you can turn the LED on and off, and you can read. Reading gives you back a RGBC (red,green,blue,clear) variable that has the readings from the sensor. See the code for how to use it.

To make this code work, before you load the code, or even open the Arduino program, we need to place the “ADJDS311″ folder into your Arduino Library. If you don’t know where that is by default, Look to the right.

If you click the download button to the right of “Arduino” you can download the whole thing as a zip, so you dont need to copy all the files.

Default Library Folder Location

On your Mac:: In (home directory)/Documents/Arduino/libraries
On your PC:: My Documents -> Arduino -> libraries
On your Linux box:: (home directory)/sketchbook/libraries

Quickly: KS0108B Graphic LCD 128×64 + Arduino

Wednesday, October 12 th , 2011

This is an incredibly quick post, and it is actually here simply because this thing was a pain to figure out how to hook it up. But, I wasn’t able to do anything with it more than the Arduino library was able to do out of the box.

So, if you have this screen sold from SparkFun, and you wanted to hook it up to your Arduino, here is how you do that.

Hooking it up

LCD screens of this type are more difficult to work with than ones specifically designed for text. With these you have individual pixel control, so you can draw completely custom graphics with it. But it also means you have to keep track of a lot of pixels, hence all the extra wires. Making matters worse, many of these screens actually require a negative voltage on the dimmer pot to get an image from it. But luckily this particular one includes all the circuitry needed.

Note: this is hookup for the non-mega Arduino. The Arduino Megas actually connect differently to this screen. Just make sure to connect all the wires to the Arduino as seen on the right. You can click on the image to get a closer look.

To drive this graphic display you need a library, and for that, Arduino has you covered: http://www.arduino.cc/playground/Code/GLCDks0108

I know this is a lame excuse for an article. But like I said, it is just here incase you need to know how to hook it up.

High Sensitivity Light Sensor TSL230R + Arduino

Friday, September 9 th , 2011

A while back we covered the TEMT6000, a great little analog sensor for getting ambient light readings when you need something a bit more sensitive than a simple Photoresistor. Well in the spirit of documenting everything we can get our hands on, this week we will be taking the sensitivity to the next level with the TSL230R Light Intensity Sensor.

I actually bought this a long time ago simply because it looked awesome. I mean really… How often do you see a clear chip? That was enough, I had to have it. Well, aside from looking downright awesome, it is also an incredibly sensitive digital light sensor. This awesome guy is sensitive enough to be used for photography light metering, or be able to tell when someone walks by a lamp on the other side of the room. Also, to top it all off, it actually has 3 sensitivity settings, so if you are in a low-light situation, it has you covered!

Hooking it up

So the TSL230R is actually pretty simple, it really only has power, and signal out. But it has a bunch of extra pins so you can change sensitivity and output type if you want. We don’t care about changing the output format, but I did want to keep the “sensitivity select” open for you incase you need it.

So just connect it up to your Arduino as shown in the image. Make sure to take note of either the larger pin in the bottom left (pin 4) or the circle on the top left (pin 1) to make sure you have it facing the right way.

Code

Before we get to far! If you are looking for a photo-ready reading from this sensor, in ISO, ASA, Lux or other, you are in the wrong place. You want this article here: Arduino and the Taos TSL230R Light Sensor: Getting Started.

If you want to be able to check for changes in light, and have some simpler code to tame this beast, keep reading.

So what the TSL230R does, is it sends out a digital Square Wave (simple up and down) signal that gets longer with less light, and shorter with more light. So we need to look at how long the wave is so we can figure out the brightness. We will actually be taking a few samples and averaging them so the signal will be more accurate and smoother. If you are looking at using a threshold to trigger something, you may want to up the number of samples it takes so it is even smoother and you don’t get false positives. (but this will slow down how long it takes to read)

This code simply checks the output of the TSL230R, and prints to the Arduino’s software serial terminal. With higher readings meaning brighter. You can change the sensitivity of the TSL230R as well. In the sketch, under setupTSL230, you can set TSL230_s0, and TSL230_s1 HIGH or LOW to make the sensor more or less sensitive to light. Great for low-light, or ultra-bright situations.

// Reports the frequency from the TSL230, higher number means brighter
// Part: http://www.sparkfun.com/products/8940
// Article:  http://bildr.org/2011/08/tsl230r-arduino/ 

int TSL230_Pin = 4; //TSL230 output
int TSL230_s0 = 3; //TSL230 sensitivity setting 1
int TSL230_s1 = 2; //TSL230 sensitivity setting 2

int TSL230_samples = 6; //higher = slower but more stable and accurate

void setup(){
  Serial.begin(9600);
  setupTSL230();
}

void loop(){

  float lightLevel = readTSL230(TSL230_samples);
  Serial.println(lightLevel);

}

void setupTSL230(){
  pinMode(TSL230_s0, OUTPUT);
  pinMode(TSL230_s1, OUTPUT); 

  //configure sensitivity - Can set to
  //S1 LOW  | S0 HIGH: low
  //S1 HIGH | S0 LOW:  med
  //S1 HIGH | S0 HIGH: high

  digitalWrite(TSL230_s1, LOW);
  digitalWrite(TSL230_s0, HIGH);
}

float readTSL230(int samples){
//sample light, return reading in frequency
//higher number means brighter

  float start = micros();
  int readings = 0;

  while(readings < samples){
   pulseIn(TSL230_Pin, HIGH);
   readings ++;
  }

  float length = micros() - start;
  float freq = (1000000 / (length / samples)) * 10;

  return freq;
}
Unless otherwise stated, this code is released under the MIT License – Please use, change and share it.

« Older Entries