G'day, I'm going to show you how to get started using your PiicoDev Ambient Light Sensor with a Raspberry Pi. We'll read light data and then remix the example code to do some decision making. Let's get started.
To follow along, you'll need a Raspberry Pi, I'm using a Raspberry Pi 4, a PiicoDev adapter, the PiicoDev Ambient Light Sensor and a PiicoDev cable to connect everything together. I recommend cables 100mm or longer for Raspberry Pi projects.
It's best if you're already comfortable using a Raspberry Pi just like a desktop computer connected to the internet. Check out our Raspberry Pi Beginners Workshop for help getting started there.
Let's connect everything together. First, plug your adapter onto the Raspberry Pi GPIO header. Make sure it's on the right way around. The Ethernet label should point towards the Ethernet or USB connectors if you're using a Pi 3. Then connect one end of your PiicoDev cable to the adapter and the other to your PiicoDev Ambient Light Sensor. I've just mounted everything to a PiicoDev platform to keep it nice and stable for this tutorial.
Make sure you have the I2C interface enabled on your Raspberry Pi. Go to the Pi menu, Preferences, Raspberry Pi Configuration. Under the Interfaces tab, make sure you have I2C enabled.
Navigate to the article for this tutorial. Now find the example script download link. That's this main.py. I'll right click that and Save Link As. It's defaulting to save to my Downloads directory, but I'll go back to my Pi home directory and create a new directory called PicoDev.
Now we're navigated inside PiicoDev and I'll rename this file from main to light, just to give it some context. Open the file.Manager and navigate to that directory we just made. There's PiicoDev and there's my light file. Double click to open in Thonny.
Let's make sure we've got the PiicoDev packages installed. Go to Tools, Manage Packages and search for PiicoDev with two eyes. There's the package and just click the Install or Upgrade button.
And this code is ready to run. So hit the green Run Current Script button and we should see some light data streaming up the console. I'm in a very bright studio so my light readings might be a little higher than yours, but if I shade the sensor I should be able to make that number diminish. We're down to about 400 lux and if I completely cover the sensor we can get to just about zero.
And on the right here you can see a small plot of this reading. So if I uncover the sensor that will jump back up and if I partially shade it it will go somewhere in the middle. You can enable the plotter by going to the View menu and selecting Plotter.
Let's take a quick tour of the code. The first line imports the light sensor driver library. The next line imports a sleep function that will allow us to create a delay. And then we initialize the sensor by calling the initialization function and we initialize the sensor as light.
Then in an infinite loop we call light.read to get the light value and store that in a variable called lightVal. We next print the data to the console. So this is printing first it's converting the number to a string that can be printed and then it's adding lux on the end with a space. And that's how we can get this print message down here all in one line. So we have lightVal and then that's concatenated with space lux.
Let'sRemix this example to create a sunrise-sunset detector. So, when we change from dark to light, it will be like sunrise, and when we change from light to dark, it will be sunset.
Create a variable called "state" and assign it the string "day". Create another variable called "lastState" and make it equal to "day" as well. By comparing "state" with "lastState", we're going to detect whether we're going from dark to light or light to dark.
Now, in this example, we're going to be comparing light to light or light to dark. In the main loop, we can create an if condition: if "lightVal" is greater than 200, then we can assign "state" as "day". Otherwise, "state" equals "night". This will update the "state" variable with the string "day" or "night" depending on whether it's bright or dark.
To check if this is working so far, we can print "state" and run the script. I'll use Ctrl+R to run it this time. Okay, so we have a high lux value and we're reading "day". If I cover it, we read "night". Very good.
Since we're updating the "state", I can check if "state" is not equal to "lastState". This means that if we've changed the state, say from day to night or night to day, then we can print some message. If "state" is equal to "day", we print "good morning". If "state" is equal to "night", we print "good night". Finally, we need to update "lastState" (correcting the typo in the process).
Now, if we run it, we're printing "we have a high lux value and it's daytime". When that lux value goes low, we turn to night. Right there, on the first instance of day to night, we print "good night".Afterwards, we just keep printing night. So we only print good morning or good night when we transition from day to night or night to day.
Now I've commented out the other print statements, so we only have the good morning or good night. And as I bring my finger closer to the sensor, we get good night. It doesn't keep repeating it. And then when I stop the script, it prints good night.
So we only print good morning or good night when we transition from day to night. It doesn't keep repeating it. And then only when I remove it does it say good morning. So we can cover to get good night, remove to get good morning. How good!
And so there you have it, a quick example script on how to just get light readings from the sensor. And then also doing a little bit of state control, a little bit of decision-making logic so that we can decide to do things either when it's bright or when it's dark. Or only when it goes from bright to dark and dark to bright.
I hope you learned something, I know I did. If you have any questions, open a thread on the Core Electronics forums or leave a comment at the bottom of this article. We're full-time makers and here to help.
I'll catch you next time.
Makers love reviews as much as you do, please follow this link to review the products you have purchased.