Carpc project - light sensors

 http://pacmans-revenge.blogspot.com/2018/04/carpc-project-light-sensors.html?m=1

 

Sunday, April 15, 2018

Carpc project - light sensors

I recently uploaded a new video to show my custom-made carpc based on a Raspberry Pi. In the video, I had to leave a lot of information out for the sake of runtime, so, I decided to write a little series of posts to explore thoughts and ideas about this project.

This episode is about light sensors.

Disclaimer (sort of):
Be aware that this post is not self-contained. If you haven't seen the video, you probably won't get much out of this reading. Also, this is not a tutorial on how to build a carpc like the one I made. For that, I'd need much more time and space. I just want to document the thought process behind a few hardware and software choices, share some techno-ramblings, and provide you with some links to get you started using the components I used.

Have you ever modified your car with custom electronics? Are you planning to hack a Raspberry Pi into a device that was not intended to be modified? Have you ever started a simple project (such as adding music to your car), and somehow turned it into a huge, titanic undertaking?

Light sensors

 

So, have you ever pulled out your smartphone in the middle of the night, touched the power button, and burned your retinas with the blazing white fury of a million suns? Or have you ever tried to read on your smartphone screen outdoors, in a sunny summer afternoon?

It's bloody annoying...

That's why I decided to implement some sort of screen brightness control for my carpc. The illumination conditions during a road trip might change drastically (starting during the day and finishing at night, entering and exiting tunnels, etc), and being forced to change the brightness of a rather big screen manually would be a deal breaker for me.

A photoresistor.
A photoresistor.
The first idea I evaluated was the use of a simple photoresistor. A photoresistor is a type of resistor whose resistance changes with the brightness of the light hitting the device. Unfortunately, most photoresistors I could find have a very uneven spectral response, which means that the variation of the component's resistance strongly depends on the color of the light, besides its intensity. I figured this would be difficult to calibrate, and I abandoned the idea.

Adafruit TSL2561.
Adafruit TSL2561.
That's when I came across the TSL2561 device from Adafruit, a very nifty light sensor module that can be easily controlled using I2C. I2C is an ideal interface, because it is natively supported by the Raspberry Pi. Also, Adafruit provides tutorials and example scripts to get you going, which is nice indeed!

Now I need to decide how many sensor to use, and where to install them. Well, the location is easy to choose: the sensors should be placed as close as possible to the screen, because that's the place where I want to measure the intensity of the light. I figured that one sensor would not be enough. I can imagine many situations in which one sensor would be fully illuminated while most of the screen would be in the shade, and viceversa. Ideally I would like to measure the intensity of the light all around the screen, but that would be highly impractical, and expensive. In the end, I settled for two sensors, one at each side of the screen.

I wrote a script based on the Adafruit examples, that uses these sensors to automatically control the brightness of the screen, and adjust it to ambient light. I want the screen to get brighter in full sunlight, and dimmer in darkness.

To achieve that, I programmed a Python script that runs a loop every 50ms. In each iteration, the script measures the brightness detected by both sensors, averages them, and compares the average against few previous measurements. If the trend is consistently decreasing, the script assumes it's getting darker, and dims the screen a little. If on the contrary the trend is consistently increasing, the script assumes it's getting brighter in the car, and it increases the brightness of the screen. Adafruit provides convenient Python libraries to generate I2C traffic on the Raspberry Pi, so reading out the sensors is literally a one-liner. The control of the brightness of the screen is also easy: I just needed to edit the system file /sys/class/backlight/rpi_backlight/brightness with a single integer number between 0 (no backlight) and 255 (UNLIMITED POWEEEEER backlight). Incrementing and decrementing the screen brightness by a single unit on every 50ms cycle allows me to smoothly change the perceived brightness of the screen, and avoid flickering effects.

I know will need to adjust a few constants to tell the script when to change brightness and how much, to make sure the system is responsive enough, and that the target brightness makes sense in each illumination condition. This, however, is not a big deal, as I can edit the script after the installation of the carpc without breaking a sweat.

Comments

Popular posts from this blog

A Short Guide To Metric Nuts and Bolts

CAN Bus Wiring Diagram, a Basics Tutorial

Best Free CFD Tutorial: FreeCAD Openfoam CFD Workbench