# Change LED Colours Using a POT and ATTINY85

117

3

Published

## Introduction: Change LED Colours Using a POT and ATTINY85

In this project we use a potentiometer (POT) to change the colours in an LED using an ATTINY85.

Some definitions -

A potentiometer is a device with a small screw / turning mechanism which when turned outputs different electrical resistances. You can see from the annotated image above that the POT has 3 pins, namely, +, - , and output. The POT is powered by connecting the + and - pins to vcc and ground respectively on a power supply. As the POT screw is turned, the output resistance changes and causes the LED to decrease or increase in intensity. . In other words, it is a variable resistor. They are used in such things as home light dimmers.

LED - This is a small light which illuminates when electrical current passes though it. In this case, we will use a multicolored LED which has 3 pins, one ground (middle) and two pins which show green and red respectively when triggered.

ATTINY85 - this is a small low-cost micro-chip which you can program like an Arduino.

Overview - The output from the POT is connected to the ATTINY85. As the POT screw is turned, a difference resistance is output as a number between 0 and 255. The ATTINY can measure this and take different actions depending on the value of the POT resistance. In this case, we have programmed it to connect to the LED as follows.

If the number is greater than 170 switch the LED to GREEN.

If the number is less than 170 but greater than 85 switch the LED to RED.

if the number is less than 85 switch on LED GREEN AND RED which results in ORANGE.

BOM

1 x 3 pin LED
1 x ATTINY 85

1 x POT (B100K)

1 power supply.

## Step 1: Programming the ATTINY85

In terms of programming the ATTINY85, please refer to my previous instructable - https://www.instructables.com/id/15-Dollar-Attiny8...

The code is shown below. Some points to note are that two ATTINY pins, PB3, physical pin 2, PB2 , physical pin 7 are connected , in digital mode, to the LED to effect colour change. ATTINY pin PB4, physical pin 3, is connected to the POT in analogue mode,which means that it can read values between 0 and 254. I customised code which I found on the internet so I acknowledge that work. - https://www.marcelpost.com/wiki/index.php/ATtiny85_ADC

```<p>void initADC()<br>{
// ***
// *** Pinout ATtiny25/45/85:
// *** PDIP/SOIC/TSSOP
// *** =============================================================================================
// ***
// ***        (PCINT5/RESET/ADC0/dW) PB5   [1]*  [8]   VCC
// *** (PCINT4/XTAL2/CLKO/OC1B/ADC2) PB4   [3]   [6]   PB1 (MISO/DO/AIN1/OC0B/OC1A/PCINT1)
// ***                               GND   [4]   [5]   PB0 (MOSI/DI/SDA/AIN0/OC0A/OC1A/AREF/PCINT0)
// ***
//pb4 - input for POT
//pb3 led pin 1
//pb2 led pin 3
// ATTINY 85 frequency set at internal 8 MHz
--------------------</p><p>     ADC Prescaler needs to be set so that the ADC input frequency is between 50 - 200kHz.

(pages 140 and 141 on the complete ATtiny25/45/85 datasheet, Rev. 2586M–AVR–07/10)</p><p>           Valid prescaler values for various clock speeds

Clock   Available prescaler values
---------------------------------------
1 MHz   8 (125kHz), 16 (62.5kHz)
4 MHz   32 (125kHz), 64 (62.5kHz)
8 MHz   64 (125kHz), 128 (62.5kHz)
16 MHz   128 (125kHz)</p><p>           Below example set prescaler to 128 for mcu running at 8MHz
(check the datasheet for the proper bit values to set the prescaler)
*/</p><p>  // 8-bit resolution
// then, only reading ADCH is sufficient for 8-bit results (256 values)
DDRB |= (1 << PB3); //Pin is set as an output.
DDRB |= (1 << PB2); //Pin is set as an output.
(1 << ADLAR) |     // left shift result
(0 << REFS1) |     // Sets ref. voltage to VCC, bit 1
(0 << REFS0) |     // Sets ref. voltage to VCC, bit 0
(0 << MUX3)  |     // use ADC2 for input (PB4), MUX bit 3
(0 << MUX2)  |     // use ADC2 for input (PB4), MUX bit 2
(1 << MUX1)  |     // use ADC2 for input (PB4), MUX bit 1
(0 << MUX0);       // use ADC2 for input (PB4), MUX bit 0</p><p>  ADCSRA =
(1 << ADPS2) |     // set prescaler to 64, bit 2
(1 << ADPS1) |     // set prescaler to 64, bit 1
(0 << ADPS0);      // set prescaler to 64, bit 0
}</p><p>int main(void)
{

while (ADCSRA & (1 << ADSC) ); // wait till conversion complete </p><p>    if (ADCH > 170)
{
PORTB |= (1 << PB3); //Pin set to HIGH.
PORTB |= (1 << PB2); //Pin set to HIGH.

}
{
PORTB |= (1 << PB3); //Pin set to HIGH.
PORTB &= ~(1 << PB2); //Pin set to LOW</p><p>    } else {</p><p>     PORTB |= (1 << PB2); //Pin set to HIGH.
PORTB &= ~(1 << PB3); //Pin set to LOW</p><p>    }</p><p>  }</p><p>  return 0;
}</p>```

## Step 2: Circuit

ATTINY pins

PB3, physical pin 2 - connected LED pin 1

PB4, physical pin 3, is connected to the middle pin POT

GND, physical pin 4, is connected to the negative rail - power supply

PB2 , physical pin 7 - connected LED pin 3

VCC, physical pin 8, is connected to the positive rail - power supply

POT

pos and neg pin connected to respective rails - power supply.

LED

middle pin connected to the negative rail - power supply

I experimented using a 3 and 3.3 volt power supply and both worked.

## Step 3: Conclusion

The capability of the ATTINY85 to move between analogue and digital mode is very powerful and can be used in a number of different applications, e.g. driving variable speed motors and creating musical notes. I will explore this in future instructables. I hope you have found this useful.

## Recommendations

• ### Large Motors Class

8,248 Enrolled

• ### Woodworking Contest

We have a be nice policy.