LDR Color Sensor
With the intention of building a cheap and simple color sensor to do projects with our students, we found an idea that seemed to allow us to do this with a simple LDR photoresistor and an RGB LED. Let's see how we combine both components and use it with micro:bit to build a simple color ball sorting machine.
The key to the operation of the color sensor is that applying for example a red light to a red object will reflect a higher intensity of light than if we apply green or blue light to the same red object. So we put the photoresistor next to the LED and emit a series of colored pulses from the latter. Reading the analogue value obtained in the photoresistor for each color pulse, we should be able to perceive differences depending on the color of the object we put in front of it, which should allow us to differentiate colors (second picture). It will not be a very accurate sensor, but well calibrated it can be useful in many situations.
Supplies
- 1x LDR photoresistor GL5516
- 1x RGB LED 5mm
- 3x 100Ω resistor
- 1x 10KΩ resistor
- 1x 1x06 pin header
- 1x 6x8 (6 strips) piece of stripboard
Circuit
The picture shows the simple circuit of the sensor. The LED simply needs the typical current-limiting resistors on the terminals corresponding to each color. The LDR photoresistor is used in voltage divider mode together with a 10KΩ resistor.
Actual Circuit
We will make cuts in three of the six strips to the stripboard in the position shown in the first picture. Note that the view of the plate is from the side where there are no copper tracks, where we will place the components. The cuts must fall under each of the 100Ω resistors.
Then we solder the components as shown in the diagram in the second picture.
The result should be similar to the 6-pin module in the third picture.
Capsule
To make sure as much as possible that the light received by the photoresistor comes from the reflection of the object we are scanning with the sensor and not directly from the LED, we designed a small enclosure for both components.
Downloads
Test Machine
To test and verify that the sensor built is valid, we designed a simple color ball sorting machine with the sensor, a pair of servos and a micro:bit. The machine consist of a sloped rail with a servo controlled end fork.
The color sensor will scan the balls so that the machine can make decisions based on their color. Servo 1 will stop the balls at the beginning of the entry lane of the machine so that the color sensor can act. It will have two positions, one for the door closed and one for the door open. Servo 2 will control the fork at the end of the entry lane and cause the balls to be routed to either the left or right exit lanes of the machine.
We make the rails out of cardboard which we attach with small 3D printed pieces that are attached to this guide. Also attached are the servo arms that we custom design.
Code
We use micro:bit as microcontroller and make the connections with sensor and servos thanks to a breakout board.
Specifically, we make the following connections:
| Device | Device pin | Color cable | micro:bit pin | |--------------|------------|-------------|---------------| | Color sensor | 1: B | Green | P8 | | Color sensor | 2: G | White | P2 | | Color sensor | 3: GND | Orange | GND | | Color sensor | 4: R | Blue | P1 | | Color sensor | 5: ADC | Red | P0 | | Color sensor | 6: +3,3V | Black | +3.3V | | Servo 1 | | | P14 | | Servo 2 | | | P13 |
The code logic of the sorting machine will be as follows:
- The starting situation is with the machine entrance door closed, controlled by servo 1.
- The program periodically scans the incoming ball position and decides whether or not there is a ball and the color of the ball.
- If there is a ball at the input, servo 2 is configured to control the fork depending on whether we want to send the corresponding color to one side or the other. We wait half a second to give servo 2 time to position itself and we open the door with servo 1, which we hold up for 1 second before closing it again, so that a new ball can be placed in the scanning position.
- If no ball is found, wait 3 seconds to allow time to insert the ball between two scanning intervals (do not insert the ball while scanning is in progress as this will most likely result in an incorrect color determination).
For the machine to work properly, several things need to be calibrated:
- RGB values read by the color sensor for each color of the balls.
- RGB values read by the sensor when there is no ball at the input.
- Servo 1 angles for open and closed door positions.
- Servo 2 angles for left and right fork positions.
For the first two calibrations with the color sensor we will emit 3 pure color pulses of 100ms and we will collect the three values that the photoresistor will read. Each set of values will identify a specific ball color or the situation in which there is no ball at the input. For example the calibration of the three ball colors used with the machine built, plus the calibration of the gate with no ball in place have turned out to be (each of the three calibration values for each color correspond to the values returned by the photoresistor when the RGB LED is switched on with red, green and blue light in that order):
- CALIB_EMPTY = (234, 242, 244)
- CALIB_RED = (103, 250, 264)
- CALIB_YELLOW = (85, 177, 254)
- CALIB_BLUE = (244, 247, 251)
The appropriate values for servo 1 have been found to be:
- ANG_DOOR_OPEN = 70
- ANG_DOOR_CLOSED = 100
The appropriate values for servo 2 have been found to be:
- ANG_FORK_LEFT = 53
- ANG_FORK_RIGHT = 73
With all these values we have written the attached program. In the video we can see the machine working configured to filter the red balls to one side and the rest of the colors to the other.