ATTiny85 - Help me understand the delay in loop

Hey all,

So, i'm playing around with the ATTiny85, and i wanted to test out the pwm for an idea I had. I'm trying to explain how this is working. The circuit is quite simple. I have an LED that is connected to VCC, the ground is connected to a transistor on pin 0 (PWM) . I have a digital read on pin 1.

The task is easy. On Pin1 going High, change the pwm from 16 to 255 to make the LED Bright. When Pin1 is low, change it back to 16.. Simple enough right? Not quite so much. As soon as I touch a jumper between VCC and ping 1 (to take it high) the LED lights up bright as it should. When I disconnect tho.. it doesn't automatically go back to 16.. it waits for a bit, almost like a hesitation, and then goes to 16, where as the trigger goes to bright right away.

The code is very very simple.

``````const int switchpin = 1; // pin 6
const int pwmPin = 0;// pin 5

int lowbeam = 16;
int highbeam = 255;
int ledVal = 16;

void setup() {
// initialize the digital pin as an output.
pinMode(pwmPin, OUTPUT);
pinMode(switchpin, INPUT);
analogWrite(pwmPin, ledVal);
}

void loop() {
{
ledVal = highbeam;
}
else
{
ledVal = lowbeam;
}
analogWrite(pwmPin, ledVal);
}
``````

Thanks all.

As it is, by your description, the input pin is "floating". Rig the input pin with a resistor to +5, like a 10K or something, that's what's meant by "tying it high". Then it's always HIGH till you touch the pin to Gnd, making it LOW. That should snap it up.

Alternately, if you don't buy that, then just take the pin straight to Gnd.

And as soon as you get a chance, google arduino pullup and pulldown resistors.

Runaway Pancake... Thanks for the heads up! Will try that next.

Raschemmel .. Thanks for the topic to read up on. These answers helped me out the most. It's forcing me to learn.

I'm thinking the final answer will be me using a transistor to my 5v switch to pull down the pin. The application i'm going to be using will introduce 5v to the switch from an external source. So, I should be able to get this to work. Will let you all know how it turns out.

Thanks again

Draw the schematic & photograh it with your cell phone and post it.

Raschemmel,

Here is the circuit I plan to build. I'll post the code later tonight once I play with the circuit a bit... But this is the plan.

I must be in the wrong window to upload a picture, but I sketched it out really quick on Circuit.io http://123d.circuits.io/circuits/196042-attiny85-led

Give that a check. I'm not sure about the pnp/npn transistor as the switch. The concept is when something else turns on and receives power, I want my chip to kick off a series of events. I'm fairly certain it should be npn for the led, but for the pin, I am torn between pnp and npn... I need to try it both ways and see what happens. Damn I love the mad scientific method of try it till it brakes..lol

Everyone should be able to see the video... but it looks like it's working. https://www.facebook.com/photo.php?v=10203103757331322

To upload a photo, click on "Additional Options" below and click on select file.

They should both be NPN. When MOSI goes HIGH the leds will light. The MISO is pulled LOW by the transistor with 5V on the base. I hope you know that doesn't mean connect the base DIRECTLY to 5V but rather that any TTL HIGH signal will turn on the transistor pulling MISO LOW. Without a HIGH on the base of that transistor, MISO is pulled HIGH by the resistor.

I need to try it both ways and see what happens. Damn I love the mad scientific method of try it till it brakes..lol

(don't let an engineer hear you say that. They like to know what's going to happen by thorough research. Trial and error comes after you've done all the research and still don't have the data you need (test results).

raschemmel,

Here is the circuit if you can’t see it in circuit.io. Thanks for reminding me how to upload a picture. I’d done it before long long ago, but must have forgot how.

I really do love trial and error… I mean, I do the research, but I also like to try stuff to see what happens. I was right on my first hunch tho to use NPN for both.

Bipolar transistors do not work that way (though FETs do)!

The base-emitter junction is a silicon diode, with a 0.6V (more or less) drop. You wouldn't connect a diode directly across the 5V power supply, so you cannot do so with a transistor base-emitter junction.

You need a resistor to limit the current, "rule of thumb" suggests 2k2 between the Arduino output and the base of the transistor powering the LEDs, and a 47k in series with the base of the transistor with the 10k pull-up. That is however, "rule of thumb" and there may be reasons to use quite different values.

JustDoc: I really do love trial and error.. I mean, I do the research, but I also like to try stuff to see what happens. I was right on my first hunch tho to use NPN for both.

But you were wrong about how to use them.

I hope you know that doesn’t mean connect the base DIRECTLY to 5V but rather that any TTL HIGH signal will turn on the transistor pulling MISO LOW.

Paul,
That’s what I meant by the above.

raschemmel:
Paul,
That’s what I meant by the above.

Yes, I saw that, I certainly know what you meant, but it seems clear he didn’t “get it”!

@Paul, Thanks for the insight, i'll go ahead and look at Mosfets for switches instead of bjts. It's all I have on hand right now to try to figure this out in this example. For the record tho, I've never pretended to know that what i'm doing is correct in any way.. So I appreciate the help, but if I don't 'get' something.. it's probably because I haven't read up on the whole thing, or had a reason to learn it yet. Many of the 'rules of thumbs' were probably taught in a class, or come with years and years of doing this stuff. Like many others that are here, I'm still a noob and just sitting around tinkering with a platform, and trying to learn it. I'll read up more on the pullup/down resistor and see if I missed something with resistors and best practices, but know that all of my 'power' in are filtered thru 7805's and wired with capacitors per the datasheet. If there is a high current on there (which there isn't) I would fix it up with the recommended circuit from the datasheets. I'm just too lazy to draw out that whole thing on a small sketch. But this is my first go around with transistors and so far, I like how they work more than anything else i've tried.

@Fungus.. Even tho it's somehow 'wrong' or 'not what you were taught is the right way' the circuit is doing exactly what I wanted it to do. It may not be right, but it's right enough for this example, which is not the finished circuit. All I'm trying to do is figure out why there was some lag in my pins.

Moderator edit: gratuitous insult removed. AWOL

Weirdly enough I can read the basic tutorials on this site and the 'Button' page says things like: *"If you disconnect the digital i/o pin from everything, the LED may blink erratically. This is because the input is "floating" - that is, it will randomly return either HIGH or LOW. That's why you need a pull-up or pull-down resistor in the circuit. " *

http://arduino.cc/en/Tutorial/Button

How mindblowing is that?

http://arduino.cc/en/Tutorial/HomePage

Moderator edit.

Nothing wrong with BJTs, except that they do require some current in the base circuit to operate while FETs do not (except for switching). I was most concerned that you do not go and wire some up directly to 5V (or for that matter, an Arduino output as there is considerable concern about damaging them by driving essentially, a short circuit) as “trial and error” can often lead to loss of “blue smoke” (either virtual or real).

If you use discrete FETs, you then have to find “logic level” ones which will work at 5V levels - switching on reliably at 3V. Clearly those in logic chips and microcontrollers and such already do this, but not so many power FETs do. A clever trick if you need a number of N-FETs in common emitter mode is to use a 74HC09 or 74HC9114 though these may not be so commonly available.

I shall leave you and “fungus” to discuss the other matter yourselves - perhaps privately would be preferable. XD

Just make sure to use a base resistor (1k) for that transistor.