Love To Code: Light Sensor: Add-On
Love To Code: Light Sensor: Add-On
Light Sensor
Add-on
#1
Written by Jie Qi
Illustrations by K-Fai Steele
Light Sensor 1
Before You Begin
This is an add-on booklet to the Love to Code Creative Coding Kit. We wrote
this booklet assuming you already have the reusable components included
in the Creative Coding Kit, as well as the Light Sensor Add-on Materials Kit.
Make space for this add-on booklet by removing the pages currently inside
your Chibi Book powered binder. Once you’ve slipped the add-on pages into
the binder, you’re ready to start!
If you haven’t purchased the Love to Code Creative Coding Kit or the Light
Sensor Add-on Materials Kit, you can get one via
chibitronics.com/lovetocode.
“Look! When I put my leaf over your circuit, nothing happens because your
circuits are not light sensitive,” said George.
1. Make the light sensor circuit on page LS-4. Instead of sticking the light
sensor permanently to the page as with the LED stickers, leave the wax
paper backing on. Place (don’t stick!) the light sensor over the copper tape.
Then, use three conductive fabric patches to stick it in place. This way the
light sensor sticker can be reused in future projects!
2. Upload the Basic Light Sensor example code to your Chibi Chip! Visit
ltc.chibitronics.com and select
Examples > Sensors > Basic Light Sensor
Tah-Dah! When the light sensor (the little bump inside the sun shape) is
made dark by covering it, pin 0 will turn on. Likewise, if it’s exposed to light,
pin 0 will turn off.
LS-2
light sensor circuit not responding? First, check if the code uploaded properly:
Make sure the stripes on Is the LED over pin 5 not turning
the Chibi Chip line up and on? Try going to a brighter spot
contact the copper tape in or shining a brighter light on the
the circuit. light sensor!
Is pin 5 not turning off when it’s Is the LED over pin 0 turning on,
dark? Check the fabric patch but not the LED sticker on the
connections and make sure the page? Press harder on the LED
light sensor is securely connected. sticker for a stronger connection.
circuit Still not working? Check out the debugging Chapter from
Love to Code Volume 1 for more tips!
+
-
S
LS-4
Fabric patches can be used to connect other stickers,
like LEDs, making them reusable too! This trick
also works to recycle old LED stickers that have lost
their stickiness. Be careful when peeling off old LED
stickers, though! The stickers won’t work if they are
bent too much.
LS-6
Decode the code
Let’s look at the example code for the light sensor to see how it works:
// Love to Code
// Volume 1: Basic Light Sensor
int lightLevel = 0;
void setup() {
outputMode(0); // pin 0 turns on and off the LED
inputMode(5); // pin 5 connects to our light sensor
}
void loop(){
lightLevel = read(5); // read light sensor from pin 5
if(lightLevel == 1) {
off(0); // if bright, turn OFF pin 0
} else {
on(0); // otherwise, turn ON pin 0
}
}
It works just like the switch code from page 3-7 of Love to Code Volume 1!
We read the light sensor like a switch. When it’s bright, the sensor reads 1
and when it’s dark, the sensor reads 0. With this information, we can use the
lightLevel variable and an if() statement to control the light on pin 0.
void loop () {
lightLevel = readLevel(5); // read sensor level at pin 5
setLevel(0, lightLevel); // then set pin 0 to
// the same “brightness”
pause(10); // tiny pause for the brightness to show
}
First we read the voltage from the light sensor on pin 5 with readLevel()
and then set the variable lightLevel to that value:
lightLevel = readLevel(5);
variable that we save pin that we are
our sensor reading to reading from: in
this case, pin 5
readLevel() gives us a number from 0 when it’s completely dark to 100
when it’s completely bright. To get information from the sensor, we use
readLevel(5), since the light sensor is connected to pin 5.
void loop(){
lightLevel = 100 - readLevel(5);
setLevel(0, lightLevel);
pause(10);
}
After uploading this new code, the darker the light sensor gets, the brighter
the light on pin 0 becomes! That’s because now we set the light level to the
opposite of the light sensor reading by subtraction:
dark:
readLevel(5) is 0
lightLevel is 100 minus 0
so, lightLevel is 100
medium:
readLevel(5) is 50
lightLevel is 100 minus 50
so, lightLevel is 50
bright:
readLevel(5) is 100
lightLevel is 100 minus 100
so, lightLevel is 0
LS-10
Cool huh? Try making your own light patterns by playing with the light
sensor! Can you get multiple pins to respond to the light sensor?
Light sensor readings can also be put inside if() conditions. Give this a try
with the Light-o-meter activity on page LS-12!
Yep! Nope.
+ -
+ -
+ -
+ -
Where’s a sunny spot for growing plants? Try taking your circuit to different
places to find out! For example:
Try checking the same places both at night, and in the afternoon! What
differences do you notice?
Light Sensor LS-15
Decode the code
Let’s take a look at how our Light-o-meter code works! Here’s the loop()
function of our example code:
void loop(){
lightLevel = readLevel(5); // read light level
if(lightLevel > 0) { // if level is greater than 0,
on(0); // turn on pin 0
} else {
off(0); // otherwise turn off pin 0
}
if(lightLevel > 20) { // if level is greater than 20,
on(1); // turn on pin 1
} else {
off(1); // otherwise turn off pin 1
}
if(lightLevel > 40) { // if level is greater than 40,
on(2); // turn on pin 2
} else {
off(2); // otherwise turn off pin 2
}
if(lightLevel > 60) { // if level is greater than 60
on(3); // turn on pin 3
} else {
off(3); // otherwise turn off pin 3
}
if(lightLevel > 80) { // if level is greater than 80,
on(4); // turn on pin 4
} else {
off(4); // otherwise turn off pin 4
}
}
LS-16
We use if() statements and the > “greater than” operator to create a light
level threshold for each LED pin. An operator is a symbol that tells the Chibi
Chip to do some kind of math or logical operation. In this case, the light level
needs to be greater than the threshold number in order for the pin to turn
on. The higher the threshold, the brighter the light level must be before that
pin will turn on.
It’s just like the height limit at a scary ride in the amusement park. In order
to go on the ride, you have to be taller than the threshold height.
== !=
> <
>= <=
LS-18
Play with code
Can you make the plant on page LS-14 grow taller the darker it gets? Try
changing your code so that the light meter goes higher the less light there
is!
Here’s a hint: try playing around with the comparison operators and the
thresholds so that pins turn on when light levels are below the threshold
number.
LS-20
Light Sensor LS-21
Draw a light-reactive scene here!
LS-22
Think outside the booK
Nice work! You’ve learned so many cool circuits and programs. Now you’re
ready to take your skills to the next level by taking your projects off the page
and outside of this book. Here are some fun things to try!
A spooky haunted
house that comes to
life when it gets dark!
A magic wand that triggers a circuit from far away, like a remote control.
Hint: send signals with a light on the wand, and receive it on the other side
with a light sensor!
Light sensors are pretty nifty, huh? Let’s dig into how they work!
First, the light sensor has to be connected to GND and +3V so that it has
power. Once powered, the light sensor produces a voltage depending on the
light level and sends it to the S pad. The S stands for signal.
Light-sensitive area
0V 3V
read(5) is 0 read(5) is 1
Meanwhile the light sensor can read any brightness level from 0 for
completely dark to 100 for fully bright, which is called an analog reading.
Analog means that we can work with values that are in between fully on and
fully off — in this case, voltages that anywhere in between 0V and 3V.
0V 1.5V 3V
The terms digital and analog aren’t just for sensors and inputs. on() and
off() are digital output functions: they turn LEDs either fully on, or fully off.
Likewise, setLevel() is an analog output function because it can make an
LED appear to be at various light levels, not just fully on or fully off.
As we learn about more types of circuits and programs, we’ll be using analog
functions more and more because we can use the additional information
along with neat math tricks to make more complex interactions. In other
words, analog values allow us to make our projects do even more cool stuff!
LS-26
“This is so cool,” said Fern. “Thanks for showing us how to use light
sensors, George! See ya later!”
“Are you leaving already?” asked George, “We’re just getting started! Can we
please keep hanging out?”
Fern thought for a second. George wasn’t always the sunniest flower to be
around, but once they started to learn things together he was actually pretty
cool. She glanced at her friends, and then knew her answer.
LS-28
Stay tuned!