Let's connect our PiicoDev Ambient Light Sensor VEML6030 to a Micro:bit and perform some light measurements. This tutorial will guide you through wiring, uploading example MicroPython code, and remixing the example to include greetings for morning and evening.

Transcript

Let's read light data using a PiicoDev Ambient Light Sensor and a Micro:bit V2. We'll get these two connected together, run some examples to read light data, and then we'll even create a little sunrise sunset detector. Let's get started.

To follow along, you'll need a Micro:bit V2, a PiicoDev Ambient Light Sensor, an adapter for Micro:Bit. and a PiicoDev cable to connect to your sensor. Start by plugging your Micro:bit into the adapter with the buttons facing upwards. Plug the cable into either PiicoDev socket on the adapter, and then plug the other end into your light sensor. I'm going to use a PiicoDev platform to keep everything in place. Connect your Micro:bit to your computer with a USB cable.

In the article for this tutorial, find the download section and right-click each link, then select "save link as". I'm saving mine to a PiicoDev folder I've made in my documents. Open Thonny and make sure you can see the file pane and the plotter by going to view, files, and plotter. Make sure those are checked. Also, make sure under the run select interpreter menu that we're running a MicroPython BBC Micro:bit and that we have MicroPython installed. If you prefer working on python.MicroBit.org, check out our guide for that too.

The workflow for each PiicoDev project is pretty similar. Start the backend if you need to connect to the Micro:Bit. and we'll upload these three files. The main.py is our example code, and the other two files are for driving PiicoDev sensors. We can select all files with a shift click and upload them to the Micro:Bit. Great! We're ready to go. We can just use control D to reboot the Micro:Bit. and we're immediately reading light data. It's pretty bright.In the studio, we're reading about 635 lux, but if I cover the sensor, I can make that fall all the way down to around 200. A little more light, a little less light, and you can see that change being reflected in the plot.

Let's have a look at what's going on. I'll stop the script with control C in the shell, and we'll work with the file that's loaded onto the Micro:Bit. So under the Micro:bit menu, select main.py.

We start by importing the ambient light sensor module, and also importing the sleep function so we can create a delay. Then we initialize the light sensor by calling the initialization function, and our light sensor is going to be referred to as light.

Then in the infinite loop, we call light.read to read the light value in lux, and we assign that to a variable called lightVal. Finally, in the print statement, we convert lightVal from a number to a string so we can print it, and we can concatenate that with lux. And that's how we can print that value with a space and the unit lux on the end. And there's a one second sleep.

Let's now remix this code to create a little sunrise, sunset alert. The Micro:bit V2 has a buzzer on board that can create some pretty cool sounds, so why don't we use that? I'll show you what I mean.

We can start by importing sounds from Micro:Bit. From Micro:Bit. import sound, and we can also import audio to drive that buzzer. We're going to need a state variable, state, and we'll assign that the string day to begin with. More on this later.

In our infinite loop, we can add the if condition, if state is equal to day, so if it's currently daytime, and lightVal is less than, let's say, 50, so if it's currently daytime, but theLight value goes below 50, that must be sunset. So we'll play a sad sound for sunset. Audio.play, sound sad, and we have to update our state variable now. State equals night. So that handles the sunset case.

Now we just need to handle the sunrise case, which is pretty similar. If state is equal to night, and lightVal is increasing, if it goes above, let's say, 200, then that must be sunrise. So we can play, like, a good morning or a hello, audio.play, sound hello, and we update the state variable. Great.

So on sunset, we'll play a sad sound, and on sunrise, we'll play kind of like a wake-up sound. I'll save that with control S, reboot with control D, and, I mean, so far, it's basically the same. We're reading 635 lux, and as I cover the sensor, that's the sad sound, and if I uncover the sensor, good morning. Awesome.

You can even add a print statement here if you prefer. That might help for debugging. There you go, I've just added some print statements. And now when it's time for sleep, it's good night with a sad face, and then good morning with a happy face when it gets bright again. How good!

And there you have it. A bit of a muck around with a PiicoDev ambient light sensor. So not only are we able to read light data, but we can do some decision-making with it and play a happy or a sad tune, depending on whether it's bright or dark.

If you make something cool from this starter project, we'd love for you to share it on the Core Electronics forums. That's also the best place if you have any technical questions or need some advice.

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.