This project walks you through adding a phototransistor (light sensor) to your Bean. The sketch will change the intensity and color of the RGB LED based on how much light is detected.
Software
- Bean Loader
- Arduino IDE
Hardware
- LightBlue Bean
- Computer
Locate these parts in your Maker Kit. You’ll need:
- 1 x 1 megaohm resistor (the color code is gold, green, black, brown).
- 1 x phototransistor
- 1 x Bean!
Place the phototransistor in with the longer leg in the pin labeled ‘0’. The shorter leg should be in the input labeled ‘A1′. Place one resistor leg in ‘GND’, the other 3 holes down and connect it to ‘A1′.
Bend the component leads on the bottom of the board. This keeps the parts in place while you flip the board to solder the leads to the gold pads. Solder the leads and then clip the excess parts.
The top side of the board should now look like this:
Load the code below in the Arduino IDE, and program the Bean. If all goes well, you should see the LED on the Bean change intensity and color when you expose it to bright light or by covering the light sensor.
How the Sketch Works- Line 1-2: we define a macro. The compiler can then substitute the token string for each occurrence in the source file.
- Line 4: is the setup function. This function is executed only once during the duration of the program.
- Line 7: we set GPIO pin 0 as an output
- Line 8: we initialize GPIO pin 0 as off (low).
- Line 11: is the loop function. This function is executed multiple times.
- Line 12: we set GPIO pin 0 as on (high).
- Line 14-15: we initialize the variable sensorValue and set it to the values from A1 pin.
- Line 16: we initialize 'r, g, b' variables as integers.
- Line 17: we filter the
sensorValue
to be above a threshold (MiNLEVEL) and below 255 (BLEVEL).
- Line 18-32: we see how
sensorValue
compares to BLEVEL or MINLEVEL and re assign 'g' and 'b' accordingly.
- Line 34-35: we set the LED accordingly to the 'g' and 'b' values and let the Bean sleep for 100ms.
Having trouble with this guide? Reach out to BeanTalk to get directed feedback from our developers and community!
Comments