Digispark Attiny85 programming help

Hey guys, Im trying to add in a Attiny85 to supply two PWM signals based on pot readings to a LED driver project.

Ive tried a few different programs and non seem to work. The more I look into it the more confused I get as to how I am suppose to program the attiny85. So I come to you for help.

I would like to use the first and second pins for the PWM outputs and the 3 and 4th pins as the analog reads of the pots. From my understanding this should be doable based on pins correct?

Here is the code I have tried that does not work-

int pwmPin = 0;
int pwmPin2 = 1;
int analogInPin = A2;
int analogInPin2 = A3;

void setup() {
pinMode(pwmPin, OUTPUT);
pinMode(analogInPin, INPUT);
pinMode(pwmPin2, OUTPUT);
pinMode(analogInPin2, INPUT);
}

void loop(){
int analogIn = analogRead(analogInPin); // Read analog voltage on pin 2 (A1)

analogWrite(pwmPin, analogIn / 4); // Output analog reading to dimmable LED
delay(5);

int analogIn2 = analogRead(analogInPin2); // Read analog voltage on pin 4 (A2)

analogWrite(pwmPin2, analogIn2 / 4); // Output analog reading to dimmable LED
}

This is code I have found online, however ive wrote and tried 4 other iterations that also did not work.

How would you code this? It seems like it should be very simple but it has bested me still.

Let me know if I've forgotten to add any needed details. Thanks all.

treahuggs:
Ive tried a few different programs and non seem to work.

Phrases like "non seem to work" and "does not work" make no sense whatsoever in a technical topic. What are you talking about? The program does not compile? The program does not upload? The program doesn't run? The LEDs don't change brightness?

Are you connecting everything properly? Take a look at this:

I do think it is something in your connections as well. Your sketch though not perfect, should be functional. Post the wiring.

Ive done bread board wiring and also attached to my project and neither work. Here is a snapshot of the project schematic-

This is what I am doing for a breadboard layout (with respect to using an actual attiny85 board)-

Codes compile and uploads fine (using code above right now). Keeping testing simple, with a breadboard, when the attiny is attached the I get zero voltage modulation and a flat zero volts on P0 when my pot is connected to P3. Here are those pictures, sorry for the messy breadboard.


Also dont mind that the LED wire isnt on the P0 pin, I took the picture first then corrected and tested again using P0 and P1 for my outputs, neither gave a voltage out at any level of the pot. I tried with 12v in and 5v in to the board (using vin).

I'm not sure what kind of breadboard you are using, but 5v sounds like the right thing to connect to Vcc, 2nd you testing A3 & A2 in you sketch but connected pots to A1 & A3 (in your fritzing diagram) how about making things a little simpler first and do a PWM test on the pins connect a led (and a 1k resistor which i am also missing in your circuit but figure it must be in there) to GPIO 0 and run this

int8_t dir=1;
uint8_t level=0;

void setup() {
  pinMode(0,OUTPUT);
}

void loop() {
  analogWrite(0,level);
  level=level+dir;
  if ((level==0) || (level==255)) dir=-dir;
  delay(5);
}

just to test PWM

Sorry about the error on the breadboard schematic, ive been getting overwhelmed with this lately.

So with this code it works on my breadboard AND my custom board. The previous attiny85 was burned up I assume.

I loaded the previous code back on. I get a output of .5volts on P0 but no modulation with the pot. however im having issues with the pot so I will have to provide another update when I go home and get a good known working pot

Looking at your code I like how it is wrote. How would you add an analog read to it? You mentioned my first code wasnt perfect, what do you think could be improved?

Thank you again for the help. This is very helpful.

By not perfect i mean, you are using excess space mainly

int pwmPin = 0;
int pwmPin2 = 1;
int analogInPin = A2;
int analogInPin2 = A3;

these can be #define literals or if you want them as variables use uint8_t (aka byte)

however im having issues with the pot

you could just measure the voltage using a multimeter to confirm that is the issue.

I get a output of .5volts on P0

you mean GPIO 0 ?! again confirm that the issue is on the output or input side, using a simple 'just output' sketch.

I did test my pot with a multimeter, thats what makes me thing its not working. Measuring my wiper and another leg doesnt give me a value but measuring my outer legs gives me my max resistance (as expected).

Since Ive become confused with all the variation names for the pins, I am referring to the pins as they are on the board. P0-P5. So P0 is the same as GPIO 0. For that instance I think the issue was the pot, I will test that more tonight or ill use just various resistor sizes while at work here.

I dont want to put in my final project yet just incase my board ive made is blowing the attinys im using. Again I am sorry about all this, I am normally way better with this stuff but been beating my head on this for about a month.

Ok so for an update-

Testing the original code from the first post with two known good pots wired pos,wiper,neg. Plugged into pin P3 on the attiny board, led plugged into P0.

Results are no modulation of the LED.

Try moving wiper to P4, same results- no LED modulation.

I also reuploaded the quick program you wrote Deva to make sure I didnt blow the board, and it does modulate the LED correctly.

Here is a picture of the breadboard.

P2 (GPIO2 ) == A1 so from the connections on the breadboard i would say your sketch should look like this

int pwmPin = 0;
int analogInPin = A1;

void setup() {
  pinMode(pwmPin, OUTPUT);
  pinMode(analogInPin, INPUT);
}

void loop(){
  int analogIn = analogRead(analogInPin); // Read analog voltage on pin 2 (A1)
  analogWrite(pwmPin, analogIn / 4); // Output analog reading to dimmable LED
delay(5);
}

and it really should work, and when it does, check what happens when you instead setint analogInPin = 2;

(Edit)Putting my wiper on P2 and the LED connected to P0 does not change the brightness. Checking the voltage on P0 it shows 0v.

Changing A1 to 2 is the same result P0 LED does not light up. I tried both pots I have.

Here a picture on the two style attiny85 boards I have.

and what are you using to power the board ? i see you have the +rail connected to vIn (which on the board must go through a regulator to deliver the ATtiny’s preferred 5v) but you have the pot connected to the same. Which might result in a voltage on the input pin higher than the supplied power, this is probably not so good and may be what is frying your pin(s) I only have the 8 pin dil versions, without any regulator on the board so i wasn’t very focused on your connections (also it is quite hard to tel from the picture.)

Im powering it through a desktop power supply at 5v, I was curious if the pot was messing with the voltage input but I did check the voltage on the gnd and vin pins, they were at 5v.

One pot is 50Kohm and the second one is 500Kohm.

You should connect the 5v to the 5v pin then, not the Vin. for the rest i can not work out what is going on.

I get the same results (no pwm) wether using 5v or vin.

I have even tried to use just different value resistors with no change.

Does this program work on your board?

Yes it does ! the program i posted in reply #9 works like a charm on my ATtiny13, checked with the int analogInPin = 2;and then the Analogread() refers to A2 (not to A0 as i was expecting) i used a 5k pot-trimmer (with a 1K between the center pin and the ATtiny input just in case i hit something while i move the wire around)

Sorry for the late reply, ive been away on business

Hmm, I think ill pick up a attiny13 then to try and see what that outcome is. I will post back once I have the 13 and tested it.

cool, keep in mind if you are using a breadboard it does help to have a 1K resistor on the middle pin of the potmeter just to limit current in case you accidentally connect it to an Output pin.

Another update for you

Amazingly this works just fine on the attiny13 and attiny13a on the breadboard.

I guess ill design a board for the 13 to drop in to my project then test that. So strange zero of the many attiny85 boards ive tested this with dont work.

That is a little odd, my Attiny85's got lost in the mail for now so i can't tell you if it is board specific as yet (they may still arrive..) If the program you are intending is not to big and you know how to make good use of your space then an ATtiny13 does the trick ay !