Analog Input with ATTINY85

It does not read in the analog value. I read the examples and tried since hours. I do not find out what am I doing wrong.
I have a Digispark ATTINY85 USB, I try to make a small temperature controller. But first I want to read in the analoge value on pin 0. It always shows 1023 or 1017 instead of the equivalent of 2V in my case which should be around 400 units. I connected the 10k NTC between A0 and +5V and a resistor of 4.7k between A0 and ground. The output does work and the serial to the notebook too which I use as readout.
My code is

Many thanks for your help Franz

/#include "DigiKeyboard.h"

int sensorPin = A0; // select the input pin for the NTC
int sensorValue = 0; // variable to store the value coming from the sensor

void setup() {
// don't need to set anything up to use DigiKeyboard
pinMode(1, OUTPUT); //LED on Oak

}

void loop() {

// Type out this string letter by letter on the computer (assumes US-style
// keyboard)
DigiKeyboard.println("ColorDisk Temperature is");

// It's better to use DigiKeyboard.delay() over the regular Arduino delay()
// if doing keyboard stuff because it keeps talking to the computer to make
// sure the computer knows the keyboard is alive and connected
DigiKeyboard.delay(1000);

//----------------------------------------------------------
//main loop Heater

digitalWrite(1, HIGH);
delay(500);

// longer off time
digitalWrite(1, LOW);
delay(100);

//----------------------------------------------------------------------
//main loop measuring NTC Temp
//Measure NTC value

// read the value from the sensor:
sensorValue = analogRead(sensorPin);
DigiKeyboard.println("Analoge ");
DigiKeyboard.println(sensorValue);

delay(1000);

}

/

That DigiSpark seems to be a little different to the usual stuff. Are you sure you are using the correct pin with A0? Check out the example Analog Read to make sure that you are right (note: no "A").

1 Like

Hi you helped me to find the solution.
This is now the program that reads in P2
Many thanks
Franz

#include "DigiKeyboard.h"

int sensorValue = 1; //initial value to see it has the value 1 or another read value

void setup() {
//You need not set pin mode for analogRead - though if you have set the pin to
//output and later want to read from it then you need to set pinMode(0,INPUT);
//where 0 is the physical pin number not the analog input number.
//
pinMode(2, INPUT);

}

void loop() {
// The analog pins are referenced by their analog port number, not their pin
//number and are as follows:

sensorValue = analogRead(1); //Read P2
//THIS IS P2, P2 is analog input 1, so when you are using analog read, you refer to it as 1.

// output the value for debugging purposes
DigiKeyboard.println(sensorValue);

delay(1000);// so you have an output every second

}

1 Like

Glad to hear that you solved the problem.
Please read the stickies at the top each forum -

and post your code within <code> tags.

Sooner or later you’ll understand why!

Happy coding!