DIY Computer Hack: Intelligent Screen Brightness Adjustment

DIY Computer Hack: Intelligent Screen Brightness Adjustment

MAKER: Ashraf Minhaj / Translated by: Qu Wujin (Please indicate the source)

Smartphones generally come with built-in light sensors, but some laptops do not have them. When you work with such a laptop in certain environments, how can you make the laptop automatically adjust the screen brightness according to the ambient light?

Here, we are going to use an Arduino and two light-dependent resistors to accomplish this task~

Through this small project, you will learn how to communicate between Arduino and a computer via serial port, as well as how to use light-dependent resistors.

Component List

Arduino development board (using Pro mini) × 1
LDR light-dependent resistor × 2
10k resistor × 1
PCB × 1
Header pin × 2
USB cable (compatible with Arduino development board) × 1

DIY Computer Hack: Intelligent Screen Brightness Adjustment

Principle Explanation

When we need to detect ambient light, using a light-dependent resistor is the simplest solution. Just like the light sensor in smartphones, point the LDR light-dependent resistor towards the person’s face to obtain the light intensity analog signal. This value is read by the ADC pin of the Arduino, with a range from 0 to 1024.

We will use a Python program on the computer to receive the light value sent by Arduino and map this value to the screen brightness level, then adjust the screen brightness accordingly.

Building the Experimental Circuit

To verify this idea, first build the experimental circuit on a breadboard. The schematic is as follows.

DIY Computer Hack: Intelligent Screen Brightness Adjustment

DIY Computer Hack: Intelligent Screen Brightness Adjustment

And write a simple program to output the light sensor data through the Arduino’s serial monitor.

// define sensor pin
int sensor_pin = A3;
void setup() {  // set things here  Serial.begin(9600);  // init serial communication at 9600 bps}
void loop() {  // mainloop  int sensorValue = analogRead(sensor_pin); // read the input on analog pin A3:  Serial.println(sensorValue);              // send data over serial    delay(200);                               // a little delay to make things work better}

The complete code can be downloaded from the project repository:

https://make.quwj.com/project/389

After running, you can see that under different light intensities, the output values range from 0 to 950.

Designing PCB

Using easyEDA to design the PCB. I selected two sensors, which makes this small device look like a snail, even cuter. In fact, the left LDR light-dependent resistor is not used.

DIY Computer Hack: Intelligent Screen Brightness Adjustment

DIY Computer Hack: Intelligent Screen Brightness Adjustment

DIY Computer Hack: Intelligent Screen Brightness Adjustment

Then just leave it to the PCB factory for prototyping.

Soldering and Completing Assembly

This board is very simple, with not many components. Just solder a header pin and light-dependent resistor.

DIY Computer Hack: Intelligent Screen Brightness Adjustment

Then plug the Arduino into the header pin.

Arduino Programming

Connect the Arduino to the computer using a USB cable, open the Arduino IDE, and upload the following code to the Arduino.

/*  Computer Hack!     Brightness Controller
    (C) License: GPL3-General Public License
    author: ashraf minhaj*/
// define sensor pin
int sensor_pin = A3;
void setup() {  // set things here  Serial.begin(9600);  // init serial communication at 9600 bps}
void loop() {  // mainloop  int sensorValue = analogRead(sensor_pin); // read the input on analog pin A3:  Serial.println(sensorValue);              // send data over serial    delay(200);                               // a little delay to make things work better}

Installing Python Program

With the above steps, the Arduino can now send light values to the computer via the serial port. The next step is to write a Python program that runs on the computer, tasked with receiving the incoming sensor values and adjusting the screen brightness accordingly.

If you haven’t installed Python on your computer yet, please download Python and complete the installation here: http://python.org/download

DIY Computer Hack: Intelligent Screen Brightness Adjustment

Open the terminal, and install two libraries, which are used for serial and screen brightness control support.

$ pip install pyserial
$ pip install screen-brightness-control

Download the source code from the project repository: https://make.quwj.com/project/389

Find controller.py, its program is as follows:

""" Computer Hack!     Brightness Controller
    (C) License: GPL3-General Public License
    author: ashraf minhaj"""
""" libraries -$ pip install pyserial$ pip install screen-brightness-control"""
# import necessary libraries
import serial                                     # for serial communication
import serial.tools.list_ports                    # to get Arduino port automatically
import screen_brightness_control as brightness    # to control brightness

# device buadrate (bit per second)
# (change buadrate according to your need)
BUAD_RATE = 9600                                  # Pro Micro's buad rate is 9600
PORT      = ""

# get sender device port automatically
serial_ports = list(serial.tools.list_ports.comports())  # get list of ports
for s_port in serial_ports:                              # iterate through all ports
    if 'Arduino Micro' in s_port.description:            # look for Pro Micro board
        PORT = str(s_port[0])                            # select first found board and
        break                                            # proceed

# connect with sender device
sender = serial.Serial(PORT, BUAD_RATE)

def map_value(value, in_min=0, in_max=1024, out_min=0, out_max=100):    
    """ To map values. Arduio sends values from 0 to 1024. My goal    is to make them in between 0 to 100."""
    return int((value - in_min) * (out_max - out_min) / (in_max - in_min) + out_min)

# mainloop
while 1:     # convert byte data into string then integer
    sensor_value = int(sender.readline().decode("utf-8"))  # get data
    final_value = map_value(value=sensor_value)            # map value (brightness in percentage)
    #print(sensor_value)
    print(final_value)    
    brightness.set_brightness(final_value)                 # set brightness

# close port properly so that others can use it
sender.close()

Run it while ensuring Arduino is also connected to the computer.

DIY Computer Hack: Intelligent Screen Brightness Adjustment

Now you can achieve the effect shown in the video!

The code used in the project can be downloaded from this project repository: https://make.quwj.com/project/386

via instructables.com/Hack-Computer-to-Make-It-Smart/

The links in the text can be clicked to view the original text at the end

DIY Computer Hack: Intelligent Screen Brightness Adjustment

More exciting content

Make a motorcycle dashboard with Raspberry Pi

Make an intelligent pet feeder with Raspberry Pi

Build an intelligent planetary observer with Raspberry Pi

Handcrafted metal wireframe X-wing clock

Arduino + 280 LEDs DIY music spectrum lamp

DIY Stanford Pupper 12 Degrees of Freedom Quadruped Robot Dog

Barrier: PC and Raspberry Pi keyboard and mouse sharing solution

DIY Computer Hack: Intelligent Screen Brightness Adjustment

Leave a Comment

×