Problems with analogRead on ATtiny85

Hi guys! I am trying programming from arduino to attiny85 via arduino as ISP.
I want to read analog values ​​with a potentiometer but unfortunately my chip reads be 0 be the maximum value.
Thank you for your answers!
(Sorry for my english I'm french)
here is the code:

int analogValue;

void setup() {
  pinMode(1,OUTPUT);
  pinMode(2,OUTPUT);
  pinMode(0,INPUT);
}

void loop() {
  analogValue = analogRead(0);
  analogWrite(1,analogValue);
  if(analogValue < 200){
    digitalWrite(2,HIGH);
  }
  digitalWrite(2,LOW);
}

Please post a wiring diagram. Post links or information about the joystick. Post your code.

1 Like

The adc's are on pins b5,b4,b3 and b2 . You have it reading from 0 which should be b0 but that does not have adc on it.

1 Like

And I suggest you change

digitalWrite(2,LOW);

to

else digitalWrite(2,LOW);

or you will be setting your output pin low immediately after setting it high.

1 Like

Thanks for your help.
I changed my program as you advised me but now I have new problems:

  1. My LED which is connected to pin 2 is constantly on, regardless of the position of my potentiometer.
  2. My led which is now connected to the ADC2 (PB4) and which receives the analogWrite only flashes in a hazard way.

Post the revised code, using code tags.

here is the new code

int analogValue;

void setup() {
  pinMode(1,OUTPUT);
  pinMode(2,OUTPUT);
  pinMode(PB4,INPUT);
}

void loop() {
  analogValue = analogRead(PB4); // the position of the potentiometer is recover
  analogWrite(1,analogValue); // I transcribe its position on an LED
  if(analogValue < 200){
    digitalWrite(2,HIGH);
  }
  else{
     digitalWrite(2,LOW);
  }
}

If the LED on pin 2 is always on, then it is reasonable to conclude that analogValue is always less than 200.

Check the range of allowed values for analogWrite().

1 Like

if i remember the adc's are 10 bits so you can read up to 1023 counts on pin pb4, then you
are writing to a pwm channel which can only handle up to 255. So you may want to use
the map function to scale the values.

serial print out analogValue so you know if your potentiometer is working correctly.

So I put a map function in my code but it still doesn't work.
The LED wich is connected to pin 2 continues to be constantly on and the other stops flashing at a certain time by going out
Here is the new code :

int analogValue;
int analogValueUse;

void setup() {
  pinMode(1,OUTPUT);
  pinMode(2,OUTPUT);
  pinMode(PB4,INPUT);
}

void loop() {
  analogValue = analogRead(PB4); // the position of the potentiometer is recover
  analogValueUse = map(analogValue,0,255,0,1023);// I transcribe its position on an LED
  analogWrite(1,analogValueUse); 
  if(analogValueUse < 200){
    digitalWrite(2,HIGH);
  }
  else{
     digitalWrite(2,LOW);
  }
}

The map function is backwards, should be 0,1023,0,255
I would forget about the led until you are able to get 0 -1023 counts back from
the potentiometer. Confirm that the pot works.

It would be a good idea to get into the habit of checking the Arduino reference pages for the functions you use, before writing the code. The above call doesn't do what you think, or anything useful, actually.

Sorry I made a mistake sending you my code.
Here is my real code:

int analogValue;
int analogValueUse;

void setup() {
 pinMode(1,OUTPUT);
 pinMode(2,OUTPUT);
 pinMode(PB4,INPUT);
}

void loop() {
 analogValue = analogRead(PB4); // the position of the potentiometer is recover
 analogValueUse = map(analogValue,0,1023,0,255);
 analogWrite(1,analogValueUse); // I transcribe its position on an LED
 if(analogValueUse < 200){
   digitalWrite(2,HIGH);
 }
 else{
    digitalWrite(2,LOW);
 }
}

so this code is not working.

I confirme that the potentiometer work.

Somethings not making sense. If your pot is working the led should turn off and on, not just stay on
Try toggling the led pin on and off in code does it blink like it should or not.

For the LED, you probably should be testing analogValue.

Post a wiring diagram.

Put in a Serial.print() to see what analog values are being returned as you vary the pot. It is probably best to write a separate program to test that part of the operation.

So I put a serial.print in my code and I observed that the value was always equal to zero moreover I changed the led of pin 1 and now it is always off.
Sorry but I don't know how to make wiring diagram

Pencil and paper work fine. If you take the hobby seriously, intend to continue, and want to learn the fundamental skill of reading and drawing schematics, this tutorial is excellent.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.