Ambient sensors suggestions (I need to get fluctuating numbers from 0 to 255)


I’m in the early stages of a project and was hoping for some ideas and input. The basic concept is an RGB lightbox given to my parents, which will display a colour based on sensors owned by me and my two sisters.

The idea is that each sensor will (somehow) provide a number between 0 and 255, with that number directly controlling the strength of its specified colour. My box will control the amount of green, while my sister Jessica will control the blue, and Emily will control the red, with the combination of the three producing an individual colour in the box my parents have.

So, I’ve been doing some research, and have some ideas on how to power the lights in the main box, as well as how to send the data across the internet (MQTT), but what I don’t know is how best to generate the strength of each of my sibling’s boxes.

The original idea was a potentiometer in each box, which we could control manually, but I’m not so keen on this idea anymore, as, while I’m sure it’ll be fun and interesting to play with when I first give them as gifts, after a while we’ll get bored of twiddling the knobs, and I’m after something which is going to keep going.

Which is what brings me here. I figured I could probably get some ambient sensors which sit inside each box, and from the data they produce, generate my required 0 to 255 number. Problem is, I’m not really sure what sensors are available, or what would provide enough range that, say, over the course of a week, the whole range of 0 to 255 could be hit, without the number changing too frequently.

I mean, I could use a temperature sensor, but it strikes me that this is going to stay pretty consistent over the course of a week. Maybe a humidity sensor? Proximity? I don’t know - which is why I’m hoping you can help.

What would you suggest? Thank you.


So, I’ve taken a look at the different sensors available for sale here at Pimoroni and think I might have found my answer - the SenseHAT.

The various sensors within it should provide a nice amount of range, and the LEDs on the front can be used for feedback (i.e. I can see just how much green I’m generating at any one point in time).

What I don’t know, however, is how best to use the data generated.

I know that video games often use very complex mathematical formulas to work out, say, how much power a character should have, based on their level, the stats on their equipment, etc, and are able to whittle this down to a single number - I’m looking to do the same, only my number needs to be within the range of 0 to 255.

Anyway, I’m wittering on, so I’ll stop. I just hope you understand what I’m talking about. Do we have any mathematical geniuses here who can help me out? Thanks.


I don’t think it takes a math genius. You need to figure out:

  • Which values you’re doing to use: light level, temperature, pressure, etc
  • What the upper/lower bounds of those values are (temperature might have a lower bound of 18c and an upper of 30c for example)
  • How much weight you want each value to have on the final value of your number

Your calculation might be:

value = get_temperature() # Maybe gives you 0 to 50 degrees C

value = min(value, max_value) # Clamp to <= max_value

value = max(value, min_value) # Clamp to >= min_value

value /= (max_value - min_value) # Divide by the difference between
                                 # max/min to get 0.0 to 1.0

value *= weight # Weight could be between 0.0 and 1.0 - 
                # a percentage of the final value that this value represents

value *= 255 # Scale up to be a portion of the 0-255 value

final_value += value # Add this value to our final value...

... and repeat

So if temperature had a weight of 0.3, pressure had a weight of 0.1 and light level had a weight of 0.6 then:

  • Temperature would make up 30% of the final value
  • Pressure 10%
  • and Light level 60%

There are infinite ways to approach this problem, though, none of which are “the right answer.” It’s really a case of tinkering with the values until you get something that feels right.


from reading your post, it sounds like you want to have 3 separate sensor reading, each controlling a single component of an RGB value tuple?

If so, wouldn’t be a simple matter of scaling appropriately each reading so that the typical variation they indvidually produce (rather than full range) output a value between 0 and 255?

Edit: me was ninjaed in style on this one ha!




Cheers guys, some good ideas here.

As you’ve suggested, I probably need to play around with the values a bit. Maybe as an initial test, I need to run the hat for a week or so, logging the values received, to see what sort of ranges I get on each of the sensors, to figure out what the natural variances are, and to see which would give the most in.

I could go one step further, doing a daily weather API call, to see what the expected temperature ranges for that day is, and update the ranges accordingly.

In any case, I think the next step is to buy a SenseHAT, rather than rely on the emulator.


I think the original idea of three pots would work, IF it was a peer-peer system. All boxes light up, and all display the same colour, but each Childs box has control of one channel.

That way when one of you tweaks their box, they all change, and you all get the same colour, which is rather sweet. You could use a rotary encoder rather than a pot (as its easier to add to a Pi), and doesn’t have a physical position. So you can set a colour with it, and then if the box is left untouched, fade the colour to black. It could fade to black over 24 hours, so at least once per day you need to turn your box up or your channel goes out!


That’s a fun idea, it might encourage input if they can see having an effect.

Of course, that now makes my life a touch harder, hahaha, but it’s a cool idea all the same :)


I have an SI1145 in one of my Pi projects. I use the ambient light reading to auto DIM the Sense Hat LED matrix in and out of low light mode. In a darkish room it dims the LED matrix so its easier on the eyes and not distracting. In brighter light it goes back to full bright. In high light conditions it toggles the text to all White. I use colors to denote conditions, but in bright sun light Red and Blue are very hard to see. White is much easier to see in those conditions. The sensor readings go from 200 - 300 in low light to readings in the thousands in bright sunlight. I just did an "if <= 200 do this, if > 200 and <= 300 do this, if > 300 do this. This Pi is a portable weather clock that shows the Date, time, temp, humidity, pressure, UV index in a scrolling message on the sense hat led matrix. Its inside and outside in very variable light conditions.

The sense hat is a really nice bit of kit, and a lot of fun to play with.