Let's connect our PiicoDev Ambient Light Sensor (VEML6030) to a Raspberry Pi Pico and perform some light measurements. This tutorial will guide you through wiring, uploading example MicroPython code in Thonny, and remixing the example to include greetings for morning and evening.

Transcript

Let's read light intensity using a PiicoDev Ambient Light Sensor and a Raspberry Pi Pico. I'll show you how to connect these two together, run some example code, and even how to remix that code to do a little bit of decision making. Let's get started.

To follow along, you'll of course need a Raspberry Pi Pico, this one with pins soldered facing down, a PiicoDev Ambient Light Sensor and expansion board for Raspberry Pi Pico, and a PiicoDev cable to connect the sensor.

Start by plugging the Raspberry Pi Pico into the expansion board, making sure that the USB connector is on the same side as the battery connector, that's the larger 2-pin connector. You can also check that pin number 0 on the expansion board is to the left of the USB connector.

On the bottom edge, you'll find the PiicoDev connector. Plug in your cable and connect the light sensor. I'm going to use a PiicoDev platform to keep everything together. And connect to your computer with a USB lead.

Now we'll download the files we need for this example. Find the example code section and right click each link and select save as. I'm saving these to a PiicoDev folder that I've made in my documents.

Open Thonny and navigate to where you saved your files. If you need a hand getting started with Thonny, check out our setup guide, also linked in the article.

Select the three files with a shift click and right click upload to root. And that is going to upload all three files to our Raspberry Pi Pico.

If we restart the Pico with control D, then we'll trigger a soft reboot and now we're reading light data. Here I'm in a pretty bright studio, so I'm currently reading about 635 lux. If I gradually shade theSensor, you can see that value decreases. We also see the plot to the right-hand side reflecting that change. Then if I uncover it, the value shoots back up. Stop the script with control C, and we'll edit the file directly on the Pico.

So under the Raspberry Pi Pico menu, double click main.py. Let's take a look at what's going on. The first line imports the PiicoDev module to drive the sensor, and then we import the sleep function so we can create a delay. Then we initialize the sensor with the initialization function and just assign that to a variable called light.

Then in the infinite loop, we call light.read to read the light value in lux, and we assign that to the variable lightVal. In the print statement below, we turn that light value, which is a number, into a string and concatenate that with lux. Then we print it out to the shell, and that's how we get that print message. And then there's just a one-second delay until the next loop.

Let's remix this code to give us a good night or good morning message whenever the light is transitioning from bright to dark or dark to bright. Just before the main loop, we can create a variable called state and assign that the string day.

Then in the infinite loop, we can check if state is equal to day and lightVal is going negative, so trending down. So if it was bright and now it's becoming dark and lightValue is less than, let's say, 50, then we can print the message good night and then set the state equal to night.

We can also run a check if state is equal to night and lightVal is greater than, let's say, 250. Then we can print good morning and set the state to day. I'll just separate these to make it a little clearer.Bit easy to read. Save the script. I'll use control S and then reboot the Pico with control D and everything looks as it did before.

We have about 640 lux, but as I cover that sensor, we get a good night message. So we can see that we just crossed about from 33 lux down to 12 and we got the good night print. Nice to see it's not continuously printing good night. It only prints it when we transition from day to night.

Now if I uncover the sensor slowly, that lux value is climbing, climbing, climbing, and once we cross 250, it prints good morning. There it is there. It's quite hard to catch that in the shell.

Now we've been working with the file that's directly on our Pico and you can tell because we're working with the file that has square brackets in its file name. If I open up the file on our computer, you can see it's the default file that we started with.

So if you want to back up your changes, right click that main.py on your Pico and just download it to your working directory and you'll see the override dialogue telling you that you're downloading a larger file and that makes sense because we've added a few additional lines of code. Click okay.

Now if we open main.py again on our computer, you can see it's backed up the changes that we made.

So there you have it. We can make a device that can detect not just whether it's bright or dark, but the changes between bright and dark. And we can get it to do, to change its behavior depending on whether it's say for example, sunrise or sunset.

If you make anything cool out of this little starter project, we'd love for you to share it over on the Core Electronics forums. It's also the best place ifDo you have any technical questions for this tutorial? And until next time, thanks for watching.

Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.