Color to frequency
A TCS3200 color sensor with two slide switches picking its red, green, blue or clear filter and a frequency counter on OUT: the more of that color, the higher the frequency. There is no microcontroller in it: the simulator solves the 4 parts as a circuit, so it runs the moment you press Run, and nothing needs compiling.
Teaching? Assign it to your class in one click.
Beginner About 15 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $7.30. On Mokxi they're free.
What you'll learn
- Light-to-frequency conversion: brighter means a faster square wave
- Choosing a color filter with two select pins
- Using a frequency counter to read a sensor
- Output scaling with S0 and S1
- Difficulty: Beginner
- Time: About 15 minutes
- Parts: 5
How it works
The TCS3200 has a grid of light sensors. Some sit behind red filters, some green, some blue, and some have no filter. The two slide switches set S2 and S3, which choose the group being measured.
The sensor turns the light from that group into a square wave on OUT. The more of that color it sees, the higher the frequency. The frequency counter shows the number.
S0 is tied to 5 V and S1 to ground, which sets the output scale to 20 percent. OE is tied to ground to turn the output on.
Parts list
5 parts, plus the jumper wires and a bench instrument. The parts retail for about $7.30; on Mokxi they're free, and so are the instruments.
- 1 × Half breadboard
- 1 × Power, 5 V
- 1 × Color sensor, TCS3200 / TCS230 (GY-31)
- 2 × Slide switchIn Logic Lab Kit, Spaceship Panel Kit.
- 1 × Frequency counter
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Color sensor, TCS3200 / TCS230 (GY-31) pin VCC | 5 V |
| Color sensor, TCS3200 / TCS230 (GY-31) pin S0 | 5 V |
| Slide switch (1) pin 3 | 5 V |
| Slide switch (2) pin 3 | 5 V |
| Color sensor, TCS3200 / TCS230 (GY-31) pin GND | Ground |
| Color sensor, TCS3200 / TCS230 (GY-31) pin S1 | Ground |
| Color sensor, TCS3200 / TCS230 (GY-31) pin OE | Ground |
| Slide switch (1) pin 1 | Ground |
| Slide switch (2) pin 1 | Ground |
| Frequency counter pin GND | Ground |
| Slide switch (1) pin 2 | Color sensor, TCS3200 / TCS230 (GY-31) pin S2 |
| Slide switch (2) pin 2 | Color sensor, TCS3200 / TCS230 (GY-31) pin S3 |
| Frequency counter pin IN | Color sensor, TCS3200 / TCS230 (GY-31) pin OUT |
The notes that come with it
There is no microcontroller in this one, so nothing is compiled: the simulator solves the circuit itself. The file in the editor is the notes that come with it.
// A color sensor read by hand: the TCS3200 turns light into a frequency.
//
// There is no microcontroller here. Press Run and the simulator solves the
// circuit.
//
// WHAT IT IS. The TCS3200 has an 8 x 8 grid of photodiodes, a quarter of
// them behind red filters, a quarter green, a quarter blue and a quarter
// bare. S2 and S3 pick which group drives a light-to-frequency converter,
// and OUT is a square wave whose frequency is how much of that color it
// sees. The frequency counter on OUT reads it.
//
// S2 S3 filter S0 S1 scaling
// LOW LOW red LOW LOW power down
// LOW HIGH blue LOW HIGH 2%
// HIGH LOW clear HIGH LOW 20%
// HIGH HIGH green HIGH HIGH 100%
//
// S0 is tied to 5 V and S1 to ground: 20%, what most sketches use. OE is
// tied to ground, which turns the output on.
//
// THE SWITCHES. The left one is S2, the right one S3. Each starts on
// ground, so the sensor starts on the red filter. Slide one over and its pin
// goes to 5 V.
//
// THINGS TO TRY. The card in front of the sensor is red (its color
// property). On the red filter the counter reads about 14 kHz; slide the
// right switch over for the blue filter and it falls to about 4 kHz. Set
// the card to white and the red filter reads about 18 kHz; set it to black
// and it falls to about a twentieth of that.
//
// IN A SKETCH. A board does the same with pulseIn(OUT, LOW), which times
// half a cycle: at 14 kHz that is about 36 microseconds. The lower the
// number, the more of that color.
Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The counter reads zero This project
Check that OE goes to ground and that S1 goes to ground with S0 at 5 V. S0 and S1 both low powers the sensor down. - You get the wrong color filter This project
Remember the order: S2 low and S3 low is red, S2 low and S3 high is blue, S2 high and S3 low is clear, and both high is green. - The color readings are random or constant. Color sensor
Set S0 and S1 for the output scaling (such as 20 percent), and S2 and S3 for the filter. Tie OE low. Read the frequency with pulseIn on OUT. - The values do not match true colors. Color sensor
Frequency is lower for stronger light. Calibrate with white and black samples and map the range. Keep the distance and lighting steady. - The input reads random values in one position. Slide switch
A switch only connects or disconnects. Add a pull-up or pull-down resistor, or use INPUT_PULLUP, so the pin has a defined level when the switch is open. - The load never turns on when the switch is flipped. Slide switch
Use the center (common) leg and one outer leg. The unused outer leg goes nowhere. Check that the switch is in series with the load. - The count reads zero or jumps around. Frequency counter
Clip the probe to the signal and share the ground with the circuit. The signal must cross the logic threshold cleanly. Slow, noisy or small signals can be miscounted. - Duty cycle or period looks wrong. Frequency counter
Duty is high time divided by the period. Check you are measuring one steady signal at the right node. A signal that changes while it is measured gives mixed answers.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.