ATTiny85 locks up after powering on LED

Go To Last Post
13 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello!

 

I've got a circuit that's not acting as I suspected it would. It's a very simple circuit (simplified from my initial, a bit more complex circuit just because I couldn't understand the error). 

 

So it's an ATTiny85, connected to an analog accelerometer outputting x,y, and z axis as a voltage between 0 and 3.3V. Input voltage to the whole circuit is 5V. I'm only using the Y-axis measurement of the accelerometer. There are two pins that go to an external board with a resistor and a LED diode. 

 

I'm measuring voltage with ADC on PB4 and sending out the current for the LED on PB3.

 

When I run the code, everything seems to work fine. I have set it to turn on PB3 when the ADC measures a voltage over a certain treshold. So I connect it to power, LED is off, I lift the device up and shake it along the Y-axis, LED turns on. 

 

But! It's supposed to turn off after the voltage is measured below the treshold again! Even when I put it down on the table again, the diode is still shining.

 

At this point, I thought something was strange with the connection and for some reason short circuiting the ATTiny, but i checked with the multimeter and even when the LED is on, the ATTiny has 5 volts over it.

 

I thought something was wrong with the accelerometer, so I changed the code and just wanted it to turn off and on the LED on PB3. I set it do delay 1s after turning on the ATTiny before it turns on the LED, then another second delay, lastly turn off the diode again. It seems to delay when I plug it in, then turn on the LED, then it gets stuck again!

 

I've attached pictures of my circuit, one of the front and one on the back. The squares show the components, lines show power and ground, dashed lines means that the connection is by wire on the front. 

 

Here's to hoping you guys can find my fault.

 

Thanks, Christian

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Oh, and the code:

#define F_CPU 1000000
#include
#include
#include
#include

void hardware_init(void){

ADMUX =
(1 << ADLAR) | // left shift result
(0 << REFS1) | // Sets ref. voltage to VCC
(0 << REFS0) | // Sets ref. voltage to VCC
(0 << MUX3) | // use ADC2 for input (PB4), MUX bit 3
(0 << MUX2) | // use ADC2 for input (PB4), MUX bit 2
(1 << MUX1) | // use ADC2 for input (PB4), MUX bit 1
(0 << MUX0); // use ADC2 for input (PB4), MUX bit 0

ADCSRA =
(1 << ADEN) | // Enable ADC
(0 << ADPS2) | // set prescaler to 64, bit 2
(1 << ADPS1) | // set prescaler to 64, bit 1
(1 << ADPS0); // set prescaler to 64, bit 0

/*-------------DECLARING PINS-------------*/
DDRB |= 0b00001000; // turn DB3 into output
PORTB |= 0b00000000; // turns off all pins
}

int main (void)
{
DDRB |= 0b00001000; // turn DB3 into output
PORTB |= 0b00000000; // turns off all pins
_delay_ms(500);
PORTB |= 0b00001000; // turns off all pins
_delay_ms(500);
PORTB |= 0b00000000; // turns off all pins
/*
hardware_init();
int reading;

while(1){
ADCSRA |= (1 << ADSC); // start ADC measurement
while (ADCSRA & (1 << ADSC) ); // wait till conversion complete
PORTB |= 0b00000000; // turns off all pins

reading = ADCH;

if(reading < 160){
}

}
*/
}

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Try inside the main function as follows.

And compare well with your code.

 

int main (void)
{
    DDRB = 0b00001000;
    PORTB = 0b00000000;
    while(1){
        _delay_ms(500);
        PORTB |= 0b00001000;
        _delay_ms(500);
        PORTB &= ~(0b00001000);
    }
}

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That did it, thanks.

 

What did I miss? Something logic related I guess?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The while(1) loop. Your code reached the end and had nowhere to go.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
PORTB |= 0b00000000;

PORTB is ORed with 0. The result is not changed.
In other words, the output will not be cleared.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 2

I'm glad that your project is now working.

 

Please add a 0.1 uF by-pass cap across the micro's power supply pins, as close to the micro as you can put it.

 

This is required for reliable operations.

 

JC

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

DocJC wrote:

I'm glad that your project is now working.

 

Please add a 0.1 uF by-pass cap across the micro's power supply pins, as close to the micro as you can put it.

 

This is required for reliable operations.

 

JC

 

Hey. Yes, you're absolutely right, thanks. This was just the first step in the construction, next is to add a voltage divider to get the accelerometer from 0-3.3V down to 0-1.1V.

 

However, I've now connected the voltage divider and it seems to make the accelerometer reading go down. At 0g I'm supposed to get ~1.66V (and I did before the voltage divider) but now it's down to 0.07V and the voltage over R1 is ~0.025 and over R2 ~0.045V. Why is this? Could it be the accelerometer not liking something in how I've connected?

 

Thanks, Christian

 

PS See picture for circuit connection

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Naccache wrote:

Could it be the accelerometer not liking something in how I've connected?

 

Maybe. Although you've not told us the model of the accelerometer or the resistor values you used.

#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Brian Fairchild wrote:

Naccache wrote:

Could it be the accelerometer not liking something in how I've connected?

 

Maybe. Although you've not told us the model of the accelerometer or the resistor values you used.

 

Model: ADXL335

 

R1 = 460Ω
R2 = 1kΩ

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I should say that it's a breakoutboard like the one shown in the attachment

 

 

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This tells you all you need to know...

 

#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Brian Fairchild wrote:

This tells you all you need to know...

 

 

That was fast, thanks. I put a single 16k resistor between the AVR and the y_out and now it works well. Thanks a lot.

Last Edited: Thu. Jan 17, 2019 - 11:24 AM