Attiny85 voltage issie

Hi all,

I'm trying to learn electronics and decided to make some mood lighting as a basic first project for my girlfriend.

I'm "Shrinkifying" the project using an attiny85, but have hit an issue.

For some reason, the pin I'm using to read the TMP36 sensor is showing the 5v and presumably assuming the temperature is much hotter than it is. If I probe the socket without the attiny85 connected, I can see that the correct voltage is coming in from the temperature sensor, but as soon as the attiny is connected, it goes to 5v.

This is the code I have. The 100C was just for debugging

Any help much appreciated :slight_smile:

#define PIN_RED 0
#define PIN_GREEN 1
#define PIN_BLUE 4
#define PIN_TEMP 3

//#define FADESPEED 100

void setup()
{
    Serial.begin(9600);
}

void loop()
{
  int reading = analogRead(PIN_TEMP);
  float voltage = reading * 5.0;
  voltage /= 1024.0;
  float temperature = (voltage -0.5) * 100;
    //Serial.print("Temp: "); Serial.println(temperature);
    int FADESPEED=50;
    if (temperature < 100) 
    {
      FADESPEED = 100;
    }
    else
    {
      FADESPEED = 5;
    }
int r, g, b;
 
  // fade from blue to violet
  for (r = 0; r < 256; r++) { 
    analogWrite(PIN_RED, r);
    delay(FADESPEED);
  } 
  // fade from violet to red
  for (b = 255; b > 0; b--) { 
    analogWrite(PIN_BLUE, b);
    delay(FADESPEED);
  } 
  // fade from red to yellow
  for (g = 0; g < 256; g++) { 
    analogWrite(PIN_GREEN, g);
    delay(FADESPEED);
  } 
  // fade from yellow to green
  for (r = 255; r > 0; r--) { 
    analogWrite(PIN_RED, r);
    delay(FADESPEED);
  } 
  // fade from green to teal
  for (b = 0; b < 256; b++) { 
    analogWrite(PIN_BLUE, b);
    delay(FADESPEED);
  } 
  // fade from teal to blue
  for (g = 255; g > 0; g--) { 
    analogWrite(PIN_GREEN, g);
    delay(FADESPEED);
  } 


}
void setup()
{
    Serial.begin(9600);
}

Which pin do you think is used for serial output?

Ah sorry that line does nothing. It's from when I had it running on the arduino.

wudzi:
Ah sorry that line does nothing.

Wanna bet?

For beginners it's much easier to "shrinkify" using one of these:

They're completely compatible with Arduino Uno (apart from not having onboard 3.3V output).

Small? Check.
$3 each? Check.
Order half a dozen? Makes a lot of sense.

You'll also need one of these to load programs into them: ftdi basic breakout for sale | eBay

(get one with a female connector, they started making them with male connectors recently...I don't know why)

It shouldn't even compile on a Tiny85.

It did compile and upload, but I've commented it out and still the same issue.

wudzi:
It did compile and upload.

Then there's a problem with your installation. I get this:

In function 'void setup()':
error: 'Serial' was not declared in this scope

I get no errors when compiling and I am not surprised.

I use this core:
https://code.google.com/p/arduino-tiny/

This code:

void setup() {
Serial.begin(9600);
}

void loop() {
Serial.println("Testing..");
}

Is how you use TinyDebugSerial on ATtiny85 (and 84/2313).
The output will be on pin PB3

@wudzi : which core are you using

Then there's a problem with your installation.

@fungus: the fact that I am not using the same core as you dosn't mean there
is a problem with my installation.

Yes, that's the core I'm using as well.

Is there an alternative pin I can move to just to be 100% sure it illuminates the issue? Bearing in mine 0,1,4 I have to use as I believe they are the only ones with PWM.

Bearing in mine 0,1,4 I have to use as I believe they are the only ones with PWM.

That's right.
The analog pins are numbered after the ADC numbers

Analog (ADC) pins are PB2 (A1), PB4 (A2) and PB3 (A3) (physical 7,3 and 2)
so for example:

int val=analogRead(2); // Reads ADC value on pin PB4

To test your circuit and the use of the right pins you could try this simplified version

#define PIN_RED 0
#define PIN_GREEN 1
#define PIN_BLUE 4
#define PIN_TEMP 3 // analog read from ADC3 - pin PB3

void setup()
{

}

void loop()
{
  // int reading = analogRead(PIN_TEMP);
  reading=5;
  for (int r = 0; r < 256; r++) { 
    analogWrite(PIN_RED, r);
    analogWrite(PIN_BLUE, r);
    analogWrite(PIN_GREEN, r);
    delay(reading);
  } 
  // fade from violet to red
  for (int b = 255; b > 0; b--) { 
    analogWrite(PIN_RED, b);
    analogWrite(PIN_BLUE, b);
    analogWrite(PIN_GREEN, b);
    delay(reading);
  } 

}

Thanks! Is there an error in there though?

// int reading = analogRead(PIN_TEMP);
reading=5;

It doesn't know what reading means because the first line is commented out - Should be comment out be on the other lines?

Anyway, I'm still having the original issue in that the pin in the socket is correct without the attiny connected, but goes to almost VCC voltage when connected. This isn't the correct behaviour is it?

No, it is not. Have you removed all references to Serial?

Yes it does it on the code above as well.

It's really bizarre - I might do a video tomorrow.

wudzi:
Yes it does it on the code above as well.

Then remove them. That is what is causing the problem.

There are no references to it in the code above (Erni's code). It does it in mine with the serial code removed as well, as I've already said.

What I would do when debugging a problem like this is to isolate the problem.
In this case I would make a sketch that only read the ADC pins to see if it was only pin PB3 that was acting strange.
(try PB2 and PB4 aswll)

Thanks! Is there an error in there though?

// int reading = analogRead(PIN_TEMP);
reading=5;

The code example was meant to test the pwm pins only, as I thought you had a problem with them.
Sorry for the confusion (in the future I will promise myself not to confuse people in here )

No problem - Appreciate the help.

Just to confirm, all the PWM for the lighting are working fine, it's only the one reading the temperature which has an issue.

OK I've had another go at this. I've basically started it all again from scratch, but for whatever reason it's still not working quite right.

What's happening now, is that the voltage on the pin is correct, but it's not being correctly interoperated. The debug the problem, I'm looking at only the lm35 sensor as I know that's what the problem is. I also know it's something to do with the circuit.

So if I use this code to debug

/*
  ReadAnalogVoltage
  Reads an analog input on pin 0, converts it to voltage, and prints the result to the serial monitor.
  Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.
 
 This example code is in the public domain.
 */

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage = sensorValue * (5.0 / 1023.0);
  // print out the value you read:
  Serial.println(voltage);
  delay(1000);
}

With the power to my circuit but the output of the lm35 to the arduino A0, I get constant 1023.

But, powering my circuit and grounding the lm35 to my Arduino, I get the correct results. So I know the problem is being caused by grounding the lm35 to my circuit.

Obviously, I've checked the connection is good. But I'm wondering if it's because the ground is shared by 12v and 5v. Is this not possible to do and use the sensor? Everything else on the circuit works perfectly.

thanks