attiny85 tutorials

Exploring ATtiny85 PWM using Arduino: Controlling LED Brightness with a Potentiometer


The ATtiny85 microcontroller, a compact and versatile member of the AVR family, is celebrated for its simplicity and effectiveness in small-scale projects. This introduction delves into the exploration of the ATtiny85’s Pulse Width Modulation (PWM) capabilities, using an Arduino as a programming interface. The focus of this exploration is a practical application: controlling the brightness of an LED using a potentiometer.

PWM is a technique used to simulate an analog output using digital means. It achieves this by varying the width of the pulses in a digital signal, effectively controlling the amount of power delivered to a device, like an LED. This technique is particularly useful in scenarios where fine control of a device’s operation is required, such as adjusting the brightness of an LED.

In this project, the ATtiny85 is programmed through an Arduino, acting as a bridge to upload the necessary code. The simplicity of the ATtiny85, combined with the versatility of Arduino’s programming environment, makes this an accessible yet educational undertaking. The inclusion of a potentiometer, a variable resistor, allows for the dynamic control of the LED’s brightness. By turning the potentiometer, the resistance changes, which in turn alters the PWM signal sent by the ATtiny85, thus dimming or brightening the LED.

This guide not only offers a hands-on introduction to PWM and microcontroller programming but also provides a practical understanding of how electronic components like LEDs, potentiometers, and microcontrollers can interact to create responsive and interactive hardware projects. Whether you are a hobbyist, a student, or simply curious about microcontrollers and electronics, this exploration of the ATtiny85 using Arduino opens the door to a world of possibilities in the realm of digital electronics and embedded systems.

Understanding PWM

The ATtiny85 microcontroller is a compact and versatile component widely used in various small-scale and hobbyist electronics projects. Its ability to perform Pulse Width Modulation (PWM) makes it especially valuable for applications where control over power delivery is essential, such as LED dimming or motor speed control. Let’s explore more about the ATtiny85 and its PWM capabilities.

Features of ATtiny85

Size and Simplicity: The ATtiny85 is part of the AVR microcontroller family by Atmel (now part of Microchip Technology). It is known for its small size and ease of use, making it an ideal choice for simple, space-constrained projects.

I/O Pins: It comes with six general-purpose I/O pins. Despite its limited number of pins compared to larger microcontrollers, the ATtiny85 is quite capable of handling a variety of tasks.

PWM Capabilities: Not all pins on the ATtiny85 are capable of PWM, but several are. This allows the ATtiny85 to control devices like LEDs and motors with simple programming.

Understanding ATtiny85 PWM

Modulating Power Delivery: PWM on the ATtiny85 works by alternating the signal on a pin between high (on) and low (off) states. The rapid switching of these states at a frequency imperceptible to the human eye is what defines PWM.

Duty Cycle Control: The key to using PWM effectively is in controlling the duty cycle – the proportion of time the signal is high in each cycle. On the ATtiny85, this is achieved through programming, where the user defines the length of the high state within the PWM cycle.

LED Brightness Control: For example, in LED dimming applications, a higher duty cycle (longer high state) will make the LED appear brighter, while a lower duty cycle (shorter high state) makes it dimmer. This is because the LED’s brightness is proportional to the average power it receives over time.

Analog Simulation with Digital Signals: One of the most significant advantages of PWM is its ability to simulate analog control using digital means. The ATtiny85, through PWM, can provide variable control over devices, much like an analog signal would, but with the simplicity and precision of digital control.

Efficiency: Using PWM for controlling power is also energy-efficient. Since the power is either fully on or fully off during each cycle, it minimizes wasted energy, which is particularly important in battery-powered or energy-sensitive applications.

Application in Projects

The ATtiny85’s PWM functionality is particularly popular in DIY projects and educational settings. It allows enthusiasts and students to learn about and experiment with basic concepts of electronics, control systems, and power management in a practical, hands-on manner.

the ATtiny85’s PWM feature is a blend of simplicity, efficiency, and versatility. Whether it’s for dimming an LED, controlling the speed of a motor, or any other application that requires variable power control, the ATtiny85 provides a compact, cost-effective solution.

Controlling LED Brightness using ATtiny85 and Arduino

Now, let’s dive into the process of controlling LED brightness using PWM with the ATtiny85 microcontroller and Arduino.


To get started, you will need the following components:

ATtiny85 microcontroller





Jumper wires

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

Code Uploading Circuit Diagram for ATtiny85 and Arduino:

I have explained this circuit diagram in my previous article, and I highly recommend reading it.

ATtiny85 PWM using Arduino

Programming the ATtiny85: Controlling Led Brightness using Arduino

Next, we need to program the ATtiny85 microcontroller. You can use Arduino IDE along with an Arduino board as a programmer to upload the code to the ATtiny85. Make sure to set the board and programmer settings correctly in the Arduino IDE. If you don’t how to set it read my article.

Here is the code to control the LED brightness:

Once the code is uploaded to ATtiny85 remove the wires.

ATtiny85 PWM using Arduino

Controlling LED Brightness using ATtiny85 Circuit diagram:

Connect the components according to the following circuit diagram:

ATtiny85 PWM using Arduino

ATtiny85 Microcontroller: This is the brain of the operation. The ATtiny85 is an 8-pin microcontroller with several I/O pins capable of PWM(pin: 3,5,6), and analog inputs(pin:2,3,5,7).

LED and Current Limiting Resistor: The LED is connected to pin 5 (PB0) of the ATtiny85, which is capable of PWM output. The yellow wire represents this connection. The resistor in series with the LED is crucial as it limits the current to prevent damage to the LED. The choice of resistor value depends on the LED’s voltage and current requirements.

Potentiometer: The potentiometer is used to adjust the brightness of the LED. It has three terminals: one connects to the positive voltage supply (VCC), another to ground (GND), and the middle terminal provides a variable voltage output. In this circuit, the green wire connects the middle terminal of the potentiometer to pin 3 (A2) of the ATtiny85, which is set up to read analog voltages.

Power Supply: The power is provided by three AAA batteries connected in series, which should give a nominal voltage of 4.5V (1.5V per battery). This voltage powers both the ATtiny85 and the potentiometer.

How the Circuit Operates:

The ATtiny85 reads the analog voltage from the potentiometer, which varies as the potentiometer’s knob is turned.

The microcontroller’s firmware is programmed to convert this analog input to a PWM signal.

The PWM signal is sent out through pin 5 (PB0) to the LED. The duty cycle of the PWM signal (and hence the average voltage across the LED) changes according to the potentiometer’s position, thus adjusting the brightness of the LED.

Code Explanation:

the pinMode() function call is used to configure a specific pin on the microcontroller as an output pin. The first argument, 0, refers to the pin number that is being configured. This is where you would specify the particular pin on the ATtiny85 that you want to control. In this context, pin 0 corresponds to the physical pin 5 on the ATtiny85, which is also known as PB0. By passing OUTPUT as the second argument, this line of code sets PB0 as an output pin, meaning that it can provide power to an external component. In this case, it is intended to control an LED. This configuration is crucial because it defines the behavior of the pin for the rest of the program, allowing the microcontroller to send signals to the LED, such as those used to adjust its brightness. This function runs once when the program starts, ensuring that the pin’s mode is set before entering the main program loop.

The loop() function is the heart of the program for the ATtiny85 microcontroller, executing in a continuous cycle for as long as the device is powered. Within this loop, the initial action taken is to read the analog value from a potentiometer via the analogRead(A2) function. This potentiometer is linked to pin A2—physically pin 3 on the ATtiny85—and this function translates the potentiometer’s analog voltage into a digital value ranging from 0 to 1023, which represents the full range of potential voltages from ground to the microcontroller’s operating voltage. Adjusting the potentiometer alters the resistance and the voltage at pin A2, thus changing the digital value produced.

Following the reading of the potentiometer, the map() function comes into play. It takes the potentiometer’s digital value and rescales it to fit within the 0 to 255 range necessary for PWM output. PWM, standing for Pulse Width Modulation, is the method by which the LED’s brightness is modulated—by varying the signal’s duty cycle. This rescaling is required because the PWM output operates at a lower resolution compared to the analog input.

With the brightness level now determined, it is applied using the analogWrite(0, brightness) function, which sends a PWM signal to pin 0—corresponding to the physical pin 5 (PB0) on the ATtiny85. This pin is connected to an LED, and the signal’s duty cycle, determined by the brightness value, dictates how bright the LED shines; a greater brightness value means a more luminous LED, whereas a smaller value dims it.

To sum up, the loop() function is tasked with a continuous cycle of reading the position of the potentiometer, converting this information into a corresponding LED brightness level, and then adjusting the LED accordingly. The net effect is that the brightness of the LED is directly controlled by the position of the potentiometer knob, allowing for a tangible and responsive interaction with the circuit.


Once the code is uploaded to the ATtiny85, you can test the circuit. As you rotate the potentiometer, you will notice the LED brightness changing accordingly. This is achieved through the PWM technique, where the ATtiny85 adjusts the duty cycle based on the potentiometer reading.


Exploring PWM with ATtiny85 and controlling LED brightness with a potentiometer opens up a world of possibilities for your projects. With this knowledge, you can create dynamic lighting effects, adjust motor speeds, and much more. The ATtiny85 microcontroller provides a compact and efficient platform for implementing PWM in your designs.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button