How to Build an AVR Night Light Circuit with Adjustable Light Level Control

AVR night light

In this project, we will build a night light circuit with an AVR chip that had adjustable lighting level control via a potentiometer.

Thus with our circuit, we'll be able to modify at what lighting level we want our night light to turn on. We can adjust it so that it turns on when it begins to dim a little or we can adjust so that it only turns on when it's pitch dark.

Again, this lighting level control is set simply by a potentiometer.

Previously, we built a night light circuit with an AVR that turned on when it did not contain any adjustable level control in its hardware. The only way to adjust the level at which the night light turned would be in the software code.

So the circuit that we are building now is really more advanced and it's an upgrade, so as we can control the hardware without having to modify code and then re-upload it to our board.

Once the level is set by the potentiometer, the LED in the circuit will turn on automatically, just as a night light circuit.

As before, the component that will be used to sense light is a photoresistor. With a photoresistor voltage divider circuit, the circuit can sense whether the ambient lighting conditions is dark or bright. If you would like to read a more in-depth explanation of this photoresistor voltage divider circuit, see the previous night light circuit example.

Components Needed

  • Atmega328/168 Chip
  • GL5537 Photoresistor
  • LED
  • Potentiometer
  • 330Ω resistor
  • 0.1μF ceramic capacitor
  • AVR Programmer

The AVR chip we will use is either the Atmega168 or Atmega328 chip. Both have the same pin layouts. If you need a reference for the pinout of the AVR chip to gain more familiarity with what each pin does, see Atmega328 Pinout. You'll need to be familiar with what each pin does in order to really know how to connect up this circuit meaningfully.

Apart from the AVR chip, we need a photoresistor. The type that we will use in this circuit is the GL5537 photoresistor. This, again, has a light resistance of about 20-30KΩ and a dark resistance of about 2MΩ. If you are going to use another photoresistor, just know its light and dark resistance and figure out the calculations necessary of how voltage will be divided up in the voltage divider circuit. You will need certain values, that we'll explain below, to go in our code so that we can program the AVR to respond to light and dark conditions, based on these value numbers.

The other components we need are the LED and the 330Ω resistor in series to limit current to the LED so that it doesn't burn out. We need a ceramic capacitor between the AVR's power pins to smooth out the power supply voltage going to the AVR. And we need an AVR programmer to upload our code to the AVR so that it can run it.

AVR Night Light Circuit

The schematic diagram of the AVR night light circuit is shown below.

AVR night light with adjustable level control

This circuit is pretty baisc.

First, we need power, as all AVR circuits do. About 5 volts of power is sufficient for operation of the AVR chip. You can get this either from batteries or a DC power supply. Vcc is pin 7 and GND is pin 8. Therefore, we connect +5V of power to pin 7 and connect pin 8 to ground on the breadboard. In between both pins, we place a 0.1μF ceramic capacitor to smooth out the power of the power supply so that the AVR chip gets a smooth power line.

Next, we have our voltage divider circuit. The end lead containing the photoresistor gets connect to the positive voltage rail and the other end lead containing the fixed resistor gets connected into the ground rail. In between these 2 components, we place a jumper wire and connect it to PC0 (pin 23) of the AVR chip. Pins PC0 to PC5 of the AVR are the analog input pins. There are 6 analog input pins in total. Being that the photoresistor is an analog component (it offers a multitude of values), it must be connected to an analog input pin of the AVR chip. With this jumper wire connected to analog pin PC0, the AVR can measure the amount of voltage that drops across the photoresistor in the voltage divider circuit.

Next, we have a potentiometer connected to our circuit. One end lead gets connected to the positive voltage line and the other end lead gets connected to ground. The middle wiper terminal of the potentiometer gets connected to analog pin PC1. Being that a potentiometer outputs an analog voltage, it must get connected to an analog pin of the AVR. Here we connect it to analog pin PC1, which will read the voltage level output by the potentiometer.

How this circuit operates is we will set the reference voltage with the potentiometer. The potentiometer will establish a certain voltage level based on our adjustment of it. If the photoresistor has a voltage value greater than the voltage value of the potentiometer, then the LED will turn on. If the photoresistor has a voltage less than the voltage set by the potentiometer, then the LED will be off.

The only other connection we make in our hardware circuit schematic is tying AVCC to VCC. AVCC is the power supply for the analog-to-digital converter inside the AVR chip. Without its own power supply, the ADC can't function. It needs its own power source in order to operate. Therefore, we must connect it to VCC for the ADC to work. Without it, we won't be able to measure analog signals because the ADC is necessary to convert the analog signal to a digital signal, which is the only type of signal that the AVR can interpret. Therefore, you can see why tying it to AVCC is the only way an analog circuit can work.

The reason we don't have to ground the ground wire of the ADC is because both grounds on the AVR chip. So being that one GND terminal is already connected to ground, we don't have to ground the ADC ground.

And the reason we don't have to tie AREF to anything is because in our software, we connect make it so that AVCC and AREF are internally connected. Therefore, AREF will be AVCC.

These are all the connections necessary for this AVR night light circuit.


Now we just need the software in order to get our circuit up and going.

The code for the AVR potentiometer circuit is shown below.

#include <avr/io.h> //Defines pins, ports, etc.
#include <util/delay.h>

uint16_t readADC (uint8_t channel) {
ADMUX = (0xf0 & ADMUX) | channel;
ADCSRA |= (1 << ADSC);
loop_until_bit_is_clear(ADCSRA, ADSC);
return (ADC);

int main (void) {
uint16_t potentiometer_level;
uint16_t lightsensor_level;
DDRB |= (1 << PB0); //Data Direction Register B: writing a 1 to the bit enables output

//set up ADC
ADMUX |= (1 << REFS0); //reference voltage on AVCC
ADCSRA |= (1 << ADPS1) | (1 << ADPS0); //ADC clock prescaler /8
ADCSRA |= (1 << ADEN); //enables the ADC

while (1) {
potentiometer_level= readADC(PC1);
lightsensor_level= readADC(PC0);

if (lightsensor_level > potentiometer_level) {
PORTB= 0b00000001; //turn on LED attached to port PB0
else {
PORTB= 0b00000000; //turn on LED attached to port PB0
return (0);//this line is never actually reached

So we first import libraries to our files. This is because this libraries contain definitions or functions we use in our code.

We then define a function, readADC(), which can be called to read analog voltage values from analog pins, which in this case is PC0 and PC1. We will call these functions later on in our code to read the analog values from the potentiometer (connected to pin PC1) and the photoresistor (connected to pin PC0).

Next we have our main() function which declares 2 16-bit integers, potentiometer_level and lightsensor_level, which will store the 10-bit ADC value from the potentiometer and photoresistor. We make the port B register of the AVR output in our code. This will enable us to turn on the LED attached to pin PB0 when we want to.

Next, we write code for the initialization of our ADC. We set the reference voltage to AVCC, we set the ADC clock prescaler to 1/8 of the clock speed, and we enable the ADC. All of this ADC initialization is done in code.

Finally, we read the call the readADC() functions to read the potentiometer level and the photoresistor level. If the photoresistor voltage level is higher, signaling dark conditions, we will turn the LED on. If the photoresistor voltage level is lower than the potentiometer voltage level, we will turn the LED off.

Related Resources

How to Use the LM741 Op Amp as a Comparator

How to Build an LM339 Quad Voltage Comparator Circuit

How to Build a Dark-activated Switch

How to Build a Hall Effect Sensor Circuit

How to Build a Touch Sensor Circuit

How to Build an Accelerometer Circuit

How to Build a Motion Detector Circuit

How to Build a Motion Detector Alarm Circuit

HTML Comment Box is loading comments...