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
#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);
}
}
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.
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?
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 )
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.