Attiny85 - Reading from ADC

Hi, im trying to switch a relay with transistor using this circuit:

But for the control i use an attiny85 programmed with Arduino UNO. The pinout of the attiny85 is:

The code i use is:

//Se eligen los pines del micro a utilizar
int ADC3=3; // El pin 3 se usa como ADC3.
int ADC4=4; // El pin 4 se usa como ADC4.
int control=1; // El pin 1 se usa como señal para activar/desactivar rele.
int val1=0; // Variable donde se cargara lo leido de ADC3
int val2=0; // Variable donde se cargara lo leido de ADC4
int max_value=5; // Maximo valor que puede tomar la tension entre terminales (5V=1023)
int read_value=0; //Variable donde se cargara el valor leido entre terminales

void setup() {
// Se configuran los pines a utilizar como entradas/salidas
pinMode(ADC3,INPUT);
pinMode(ADC4,INPUT);
pinMode(control,OUTPUT);
digitalWrite(control,LOW);
}

void loop() {
//El ADC del attiny85 tiene 10 bits de resolucion, es decir mapea valores de 0 a 5V (VCC), entonces tiene una resolucion de 5/1024=0.0049 (4.9 mV)
//Se lee el valor de los ADC
digitalWrite(control,LOW);
delay(3000);
val1=analogRead(ADC3);
delay(500);
val2=analogRead(ADC4);
delay(500);
//Comienza la comparacion
read_value=val1-val2;
//Si el valor entre los terminales del parlante es mayor a 20mV entonces desconecta la carga, enciende un LED avisando y finaliza la operacion.
if ((abs(read_value))>max_value){
digitalWrite(control,HIGH);
delay(3000);
}

}

The problem is that if val1>val2 the relay switch and its all ok, but if val2>val1 the relay doesnt switch. I try that first putting VCC on pin3 and GND on pin4, and next VCC on pin4 and GND on pin3 (and that doesnt work). I put a decoupling capacitor of 100 nF between VCC and GND near the attiny85. The value that i use for "max_value" is only for testing, i will change it to a value that represent 100mV. Another thing is that i only test the circuit in a protoboard. Any suggestion? Thanks.

PS: Sorry for my english.

It can be a bit confusing with ATtinys sometimes. :slight_smile:

ATTiny85 has 3 ADCs (Analog to Digital Converters):
ADC1 is on physical pin 7 (see the picture of your post: "Analog Input 1")
ADC2 is on physical pin 3 ("Analog Input 2")
ADC3 is on physical pin 2 ("Analog Input 3")

Whith this statement

int value = analogRead(1);

you read "Analog Input 1" - it is on physical pin 7

Do not get confused by the fact that physical pin 7 can also be used as "digital pin" and you can do this by using a statement like this:

digitalWrite(2, HIGH);

So: same physical pin, different "name" for analog and digital functions. Confusing :slight_smile:

In your sketch you try to read ADC4 ("Analog Input 4") whitch does not exist.

Change the code like this:

int ADC3=3;   // El pin 3 se usa como ADC3. Physical pin 2
int ADC4=2;   // El pin 4 se usa como ADC2. Physical pin 3

Pin mapping may depend on the core you use. But most cores work like described.
I use DrAzzy's core and I highly recommend it:

It works, thanks!

Thank you for the follow up.

Your schematic is lacking a flyback diode across the relay coil to prevent destruction of the npn transistor each time the transistor opens the 12 volt circuit and the energy stored in the coil is released. Any general purpose silicon diode with a 100 volt prv rating should suffice.