The Adafruit Circuit Playground Express comes equipped with an analog light sensor, but it can be used for much more than just sensing light or darkness! The light sensor has a similar spectral response to the human eye. Its connected to analog pin A8 and will return a value between 0 and 1023. A normal indoor light level reading is about 300, with higher numbers being brighter.

Transcript

Hi, Steven here from Core Electronics! Today I want to show you how to use the light sensor on the Adafruit circuit playground Express to make a colour matching light wheel and we use circuit Python in order to program it.

The circuit playground Express has an analog light sensor cooked right into the board, in the top left and that returns an analog signal of just 0 to 64,000 and you might wonder how we're going to take just as 0 to 64,000 number and turn that into a colour. Well in order to do it you've got to understand how colours work so when we look at a green object that object is absorbing every colour in the visual spectrum except green and whatever other parts of colours that make it its exact colour. But when I look at this green owl, I'm seeing the green light reflected off it, likewise when you see blue that objects reflecting more of the blue wavelength than any other wavelength.

So on the Neopixel board, the Neopixels on the circuit playground Express, there's a Neopixel right next to the light sensor so we can have it turn on red and take a reading from the from the light sensor while the LED is lit up red and if we have a red object over it then it will reflect more red light than any other light. So, I'll give a quick demonstration when we do a sense, it turns the light on red, green and blue quickly and takes a quick reading while each colour is on in order to sense the object above it. So if we put a blue object above it then more blue light is reflected off the object to the light sensor and we're able to create an RGB value from that, if we put a red object over it then more red light will be reflected than anything else and likewise for any other colour. So, our end code will be a sense from the button B press and the left button will clear any lights that we have on currently. Now this isn't a perfect system for a reading colour as you can see by the first try on this yellow object it came up green and there it's red but considering we're just using a light sensor and an LED it works pretty good, so let me show you how we made it.

So, if we open the code in Mu, I'll break down the different parts. First, we need to import all our libraries for this to work. We use switches on the board, we're using analog inputs, so we need our analog in and out libraries and we're going to remap some of our values from the analog sensor to a 0 to 255 sensor later on so we'll use a simple in our map range library as well. We'll start by initializing our Neopixels so we'll be able to use them and this is an important step right here we have our analog in board light, so that's creating our analog input from the light sensor and storing it as a variable analog in that we'll use throughout the sketch and the main body of our sketch we have an if statement that contains the main body of our code so that's if circuit playground express button B, so if button B's pressed then it'll run our check for colour sensing, first of all we'll turn all our LEDs off because if there's lights on when we do the check those other lights would stay on and it would throw off our readings if we had all green lights and they're trying to read something that needs to be very small amounts of differences can throw off your results completely which is why you'll need to try once or twice before it matches the colour properly and then at this part of the code we turn each light on in turn and then read the analog input from the light sensor while that lights on then we have a short pause and we turn them off and we really want the pause there just so there's time for the Neopixels to react because they don't they don't light up full brightness instantly there is a slight delay.

The next part of our sketch we determine our highest and lowest light readings. So whatever reading that we take while we're checking each colour, the highest one will become the maximum part of our range and the lowest one will be the lowest part of the range that will convert from and we write here we remap from the values that we read from the raw sensor (the raw data). We’ll jump ahead and look at our serial monitor down here. Maximum light received from the analog sensor was 14,272 and in the minimum light the lowest reading was 1,488 so our range of light is from 1,488 to 14,000 and we need to convert that to pH which would be a value of between 0 and 255, which is a value that the Neopixels recognize.

Now the obvious problem with making the minimum and maximum of our range, the highest and lowest reading is that our lowest reading will be returned as a 0 in the new range and our highest reading will be turned returned as 255. So, that obviously takes out a lot of accuracy within our reading however Neopixels won't be able to take an exact reading from our colour sensor and you won't be able to take an exact RGB reading and plug it straight into Neopixels and have that appear to be the same colour on the lights. In order to change our raw data from the light sensor into a 0 to 255 range we'll use the map range command and within the brackets we have our data input, the minimum original range, the maximum original range and then we have the new range that we need to map it into and that's going to be between 0 and 255.

Now the problem with using the minimum recorded value and the maximum recorded value from each reading is that the minimum will be returned to 0 and the maximum will be returned as 255 and the third middle number is the only one that's going to have a changeable value but when you try to turn a true RGB reading off the light sensor into Neopixel lights. Then it usually ends up being mostly white or completely washed out and because our eyes don't see a colour that a Neopixel puts out the same as it receives colour signals. So in order to get colours from the circuit playground express that are more like what we'd expect to see or that are closer to the colour of the actual object, we need to simplify it so having 0 for one value 255 for the other and the variable third value is the best way to get colours to show up how you'd expect them to show up and to simplify them further we're going to check red, green and blue, if any of them are under 30 then we're going to just change them to 0 and we do this because if we want to read something that's red it's almost always going to have some other reading on the other colours involved.

So, if you have 255 on red it'll have 10 to 20 on something else and that'll make your colour look to your eye completely different than red even though it's remarkably close to being a perfect reading but if we take the bottom 30 out and then it is able to default to just a true red, green or blue when it's really close otherwise we won't ever be able to get those colours and you don't notice so much in any in-between colours if there's a little bit of variability there. The last thing we do is we convert our red, green and blue to integer values because currently there are floats, so they have lots of decimal places in there but we can't have decimal places in our Neopixel output and we have a serial monitor print so we print our maximum light, our minimum light, our raw RGB values that we've read and then our calculated RGB values that we use for the Neopixels and then if our minimum and maximum read values are less than or equal to 6000 then we don't fill anything at all because if there's no object above it we don't want to display a random colour every time we just want the pixels to stay off, so then you know you can do another reading again and if then if it is a good reading then it will go ahead and show the colour on all the Neopixels and fill them in this command and then output the colours that are red.

So, if we take a look at the serial monitor we can see that measuring the light off of this blue octopus, we have our maximum and minimum light our raw values and then our converted output which is 0 red, 71 green and 255 blue and it ends up being pretty close. So we will do it again for yellow so we have 172 on red, 255 on green and 0 on blue and it makes a pretty good match so you can't do perfectly accurately any colour using this method but you can do most colours and certainly enough that it's easily recognizable that you've matched the colour. So, if you're an educator I think this is a really good project because it's it brings a level of interactivity to the world around you that usually don't experience so there's not a lot of off-the-shelf products out there that would allow you to duplicate the colours of things that you see and if you're making it as a wearable, it's a great option for a wearable because there's a lot of potential there with being able to match your environment change the look of what whatever wearable you've created.

That concludes my tutorial on how to use circuit Python and a light sensor on the Adafruit circuit playground Express to make a colour matching light wheel. If this is a project that sounds like a lot of fun for you but the programming is a little bit over your head check out my other tutorials on how to do the same project using MakeCode, this is a great platform for educators and beginners and you can get real satisfying results from it just like this with only a few minutes with no programming knowledge required.

Comments


Loading...
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.