Go Down

Topic: analogRead function in ATtiny chip (Read 12762 times) previous topic - next topic


Jan 12, 2012, 07:24 pm Last Edit: Jan 12, 2012, 07:27 pm by mechatron Reason: 1

When using this code in my Arduino ATmega it works fine and the moment of engagement can be trimmed with a potmeter.

Code: [Select]

code to use 2 leds as brake lights on RC car
If the esc/throttle servo has to be trimmed
the pot meter can be used to set the moment
of engagement to the desired value
by Leo Groeneveld, dec 2011

const int ledPin = 0;

int ledState = LOW;
int x;                          // variable to store trigger value
int y;                          // variable to store pulse width

void setup()
pinMode(1, INPUT);              // Pin 1 (3) set as input to read the PPM signal from RC receiver for the UNO use the number between parentheses
pinMode(2, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, ledState); // turn/keep leds off at start

void loop()
int sensorValue = analogRead(2);      // Read value from resistor
x = map (sensorValue, 0, 1023, 1000, 1500);
y = pulseIn(1, HIGH);

if(y < x && ledState == LOW){
 ledState = HIGH;
 digitalWrite(ledPin, ledState);
   if(y > x && ledState == HIGH){
     ledState = LOW;
     digitalWrite(ledPin, ledState);

When using the same code in an ATtiny45 the potmeter does nothing.
The ATtiny should be able to do an analogRead on pin 2 but it doesn't.

Should the code be changed for an ATtiny?

EDIT: solved by using pin 3, but pin 2 should be able to work also according to this image:

to boldly code what no man has coded before

Coding Badly

Analog 2 is the same physical pin as Digital 4.  Analogs are reference by their ADC number (ADC2 is Analog 2).  Digitals are referenced by their bit number (PB4 is Digital 4).

Do not call pinMode when using pins for analogRead.


Aug 17, 2012, 08:05 pm Last Edit: Aug 17, 2012, 08:10 pm by BlueJakester Reason: 1
I was able to control a shift register with an ATtiny45, making LEDs blink at intervals based on analogRead() from a potentiometer. But not before finding this thread!

Using the same  number (2) for both "SCK" and "ADC2" analog input was a new and confusing experience. Somehow the core code must differentiate based on usage. Below is a snippet of my code's declarations.

Without searching and finding this thread I doubt I would have solved it. Thanks to member Coding Badly for answering this back in January!

Code: [Select]

// ATtiny45 to 74HC595
// pp = physical pin

int data  = 0; // (pp5) DATA to 14 Serial Data Input on 74HC595
int clock = 2; // (pp7) SCK  to 11 SH_CP clock on 74HC595
int latch = 3; // (pp2) Analog 3 to 12 ST_CP Latch on 74HC595

// pp 4 ground to 13 OE on 74HC595

// for potentiometer
int sensorPin = 2; // (pp3) ADC2 Anlalog Input 2  pin for the potentiometer
int sensorValue = 0;


I am sorry for reviving this topic, however, I must say I second this.
This pin labeling uncertainty is a bad apple in otherwise great thingy. :3

I am saying it after having debugged this code:

Code: [Select]
int analogReadPin=2
digitalWrite(analogReadPin,HIGH);//internal pullup on;
int analogValue=analogRead(analogReadPin);//read on pin 2

This code produces an error-it sets internal pullup thingy on DIGITAL pin 2, then attemps to read from ANALOG pin 2
correct code is:

Code: [Select]
digitalWrite(4,HIGH);//internal pullup on;
int analogValue=analogRead(2);//read on pin 2

Which is rather inconvenient.
I am sorry if it already has been solved, I am too busy to check it out at the moment and would forget about posting this once I am not. xD

Go Up