Engineers Garage

  • Projects and Tutorials
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Products News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • White Papers
    • Webinars
  • EE Learning Center
  • Women in Engineering

Adaptive Screen Brightness using LDR and Python script

By Varun Kumar

Requirements:

1.     Arduino with Atmega328p

2.     LDR

3.    Python 2.7 on your machine

4.    Python libraries: Pyserial

Summary:

Ever wondered how useful is the automatic brightness feature on phones? It can quickly  alter screen’s brightness according to the surrounding light intensity.

In this project I have tried to create something similar for any PC or Laptop running on Linux (preferably Ubuntu 14.04).

My goal was to keep it  affordable and simple as much as possible.

So to make its prototype, I used LDR to measure light intensity and an Arduino to communicate with my machine. On my machine I used a python script to communicate over USB with Arduino and to edit the file which is responsible for the brightness of screen.

This project costs around Rs 300 just because of microcontroller that is being used (needless to say it’s a prototype). It has got very high utility,  especially for PC users.

To get more detailed information on controlling brightness using terminal, you can checkout this link.

And to get more detailed references on atmega328p you can checkout Atmega328p documentation.

Description:

How I am uploading code on arduino:

f = <source_code’s_file_name>

avr-gcc -g -mmcu=atmega328p -Wall -Os $(f).c -o $(f).elf

avr-objcopy -j .text -j .data -O ihex $(f).elf $(f).hex

sudo avrdude -F  -V -c arduino -p m328p  -P /dev/ttyUSB* -b 57600 -e -U flash:w:$(f).hex

Just type these four commands, in the same order, in your terminal and remember to put the source code’s filename in variable “f”. These commands are for Linux users only.

First command stores the filename in variable “f”, second command is used to convert source code to .elf file, third command is used to convert that .elf file to .hex file which can be uploaded on atmega328p, and the fourth command is used to upload that .hex file.

Little bit about LDR that is being used

LDR stands for Light Dependent Resistor.  Its resistance has an inverse relationship with luminous intensity. Generally, it has very high resistance somewhere around 1000 Kohms but when light falls on it,  it decreases dramatically by few hundred ohms. LDR is an inexpensive device which finds its way in many of our day to day applications.

Explaination of Source Code

This program is an application of ADC and serial communication.

Before reading this article, check out the explanation about ADC here.

There are few changes that I made in setup_adc() function and ISR() block.

In setup_adc() function I set the MUX1 bit in ADMUX register in order to start the conversion of value at PORTC2.

In ISR() block, I first stored 10-bit value from ADC in a variable(ldrValue). Then using conditional statements I mapped “ldrValue” to three levels of brightness:

a.    minimum_brightness = 500

b.    medium_brightness = 2500

c.    maximum_brightness = 4880

*You might need to change them according to your machine.

I have used uartLibrary.h to send brightness value and ldr value to my laptop using serial communication. I built this library referring to this article . After including this library I can use printf() function from stdio.h to send strings from atmega328p to my laptop.

I put all code files in a .zip folder

For detailed information please check the Source Code it’s very well documented.

Explanation of Python Script

I’m using pyserial  libraries for serial communication, it’s freely available.

Python script needs root permission to run  properly; else it will display an error message and exit.

Once it gets the root permission it creates an object “ser” of “Serial” class and takes two arguments in it:

a.    Serial Port: In my case it is ‘/dev/ttyUSB0’

b.    Baud rate: In my case it is 9600

Then, it sets the file path in a variable (path).

*You might need to edit them according to your machine.

Then in an infinite loop, I put a try & except block just to take care of initial garbage values.

In try block, a variable (value) stores the list of two strings, one is the brightness value other is the LDR value.

After that it opens the file given in “path” variable in write mode and writes the brightness value in it. Then it saves and closes the file.

And for debugging purpose, it prints the list with brightness and ldr value.

For more information, you can also check this link changing brightness through terminal.

Typical Image of Arduino Uno used for Automatic Control of LED Monitor's Brightness

Fig. 1: Typical Image of Arduino Uno used for Automatic Control of LED Monitor’s Brightness

Typical Image of Arduino Uno

Fig. 2: Typical Image of Arduino Uno

Project Source Code

###

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//                                                          Varun13169                                                                //

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 

#define F_CPU           16000000UL

#include "uartLibrary.h"

#include

#include

#include

#include

 

 

/****************** Limits **************/

#define low_min 0

#define low_max 10

 

#define mid_min 10

#define mid_max 30

 

#define high_min 30

 

#define minimum_brightness 500

#define medium_brightness 2500

#define maximum_brightness 4880

/****************************************/

/*

 You might need to change these limits

 according to your Linux machine and LDR

*/

 

 

int ldrValue=0;

void setup_adc();

 

int main(void)

{ 

    uart_init();

    stdout = &uart_output;

    stdin  = &uart_input;

   

    DDRC |= 0x0A;                  // Set Pin5 and Pin7 on arduino as output

    PORTC = 1<

   

    sei();                        // Enales global interrupt

    setup_adc();            // Setup ADC according to the defined function

             

    while(1)

    {

    }

}

 

 

 

void setup_adc()

{

    ADCSRA |= 1<

    ADCSRA |= 1<

    ADCSRA |=  1<

    ADMUX |= 1<

    //ADLAR=1 for left adjusted result and REFS0=1 with REFS1=0 to use Vcc as reference voltage

    //MUX1=1 because we want to start conversion from PORTC2

    DIDR0 |= 1<

}

 

 

/*********** varun13169 **************/

 

 

/******************************************** ISR Block ********************************************/

ISR(ADC_vect)

{

    uint8_t adcl = ADCL;                                  //This is an 8-bit varible used to store the value of ADLC

    uint16_t adc_ten_bit_value = ADCH<<2 | adcl>>6;  //This is an 16-bit varible use to store the 10-bit value of left adjusted result

   

    ldrValue=adc_ten_bit_value;

   

    if(low_min<=ldrValue && ldrValue

    if(mid_min<=ldrValue && ldrValue

    if(ldrValue>=high_min){printf("%d %dn", maximum_brightness, ldrValue);}                    // to my laptop

 

    ADCSRA |= 1<

}

/***************************************************************************************************/

 

 

/*********************************************************** END *******************************************************************/

###

 


Circuit Diagrams

Circuit-Diagram-Arduino-Uno-Based-Automatic-LED-Monitor-Brightness-Controller

Project Video


Filed Under: Electronic Projects

 

Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


Featured Tutorials

  • Designing Gate Driver Circuit and Switching Mechanism for Modified Sine Wave Inverter – (Part 9/17)
  • Completing Modified Sine Wave Inverter Design with Full Bridge Circuit and Step Up Transformer – (Part 10/17)
  • Designing an Offline UPS – Part (12 /17)
  • How to reduce Switching Time of a Relay – (Part 15/17)
  • Testing MOSFET – (Part 16/17)
  • Driving High Side MOSFET using Bootstrap Circuitry – (Part 17/17)

Stay Up To Date

Newsletter Signup

Sign up and receive our weekly newsletter for latest Tech articles, Electronics Projects, Tutorial series and other insightful tech content.

EE Training Center Classrooms

EE Classrooms

Recent Articles

  • Infineon offers DC-DC controller for full LED headlamps without a microcontroller
  • Vishay launches new high-precision, thin-film wraparound chip resistor
  • STMicroelectronics’ common-mode filters ensure signal integrity in serial interfaces
  • Renesas’ RA Family microcontrollers earn CAVP certification for cryptographic algorithms
  • MicroPython: Serial data communication in ESP8266 and ESP32 using UART

Most Popular

5G 555 timer circuit 8051 ai Arduino atmega16 automotive avr dc motor display Electronic Part Electronic Parts Fujitsu ic infineontechnologies integratedcircuit Intel IoT ir lcd ldr led maximintegratedproducts microchip microchiptechnology Microchip Technology microcontroller microcontrollers mosfet motor powermanagement Raspberry Pi remote renesaselectronics Research robot samsung semiconductor sensor software STMicroelectronics switch Technology vishayintertechnology wireless

RSS EDABOARD.com Discussions

  • Level shifter for differential oscillator
  • Controller for Switched Reluctance Motor
  • Boost converter, 3W...shielded or unshielded inductor?
  • IRF450 MoSFET
  • kt/c noise of sample and hold

RSS Electro-Tech-Online.com Discussions

  • Passthrough charging-simple but impossible to achieve?
  • Relay connectors unknown?
  • Background of Members Here
  • Will Header and socket hold this PCB OK?
  • software PWM
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • About Us
  • Contact Us
  • Advertise

Copyright © 2022 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Projects and Tutorials
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Products News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • White Papers
    • Webinars
  • EE Learning Center
  • Women in Engineering