ATTiny85 timer1 output voltage on PB4 is 1.2V instead of 5V

Hello,
I have written below code to output 500KHz square wave on PB4 pin. I am getting the waveform generated at right frequency but the peak to peak voltage is only 1.24V. I would like it to be at 5V.

Strangely, when I also set PB3 as an output pin, the peak to peak output on PB4 is coming at 5V.

I would not like to set PB3 as output as I need that pin configured as input for some more logic I will add later.

Hence would someone let me know how to get 5V output at PB4 without needing to have PB3 also as output?


void setup() {
  // timer 1
  DDRB |= (1 << PB4); 
          // | (1 << PB3); // uncommenting this makes PB4 peak to peak output goto 5V
  setupTimer1();
}

void loop() {
}

void setupTimer1() {
  noInterrupts();
  // Clear registers
  TCCR1 = 0;
  GTCCR = 0;

// Enable PLL and async PCK for high-speed PWM
  PLLCSR |= (1 << PLLE) | (1 << PCKE);
  delay(10);
  
 //50% duty cycle
  OCR1C = 3;
  OCR1B = 1;
  // Prescaler 32
  TCCR1 |= (1 << CS12) | (1 << CS11);
  
  GTCCR = (1 << PWM1B) | (1 << COM1B1) | (0 << COM1B0);
  // Output Compare Match A Interrupt Enable
 //TIMSK |= (1 << OCIE1A);
  interrupts();
}

//ISR(TIMER1_COMPA_vect) {
//  
//}

Per the datasheet,

I have COM1B1 set and COM1B0 cleared to have PB3 (~OC1B) not connected to the timer1. So not sure why setting this as output is having effect on output voltage of PB4. Even if I make PB3 as output, I don't get any waveform on PB3 (as expected).

regards,
aquaman

Try removing that line. The datasheet doesn't say that you the PWM pin has to be defined as an output.

1 Like

Hello @pylon
Thank you for replying.

I commented that line out (effectively making PB4 an input). Now there is no waveform on pin PB4. I just get a flat line at 0v in the Oscope on PB4.

Thanks
aquaman

How do you know it is 1.24V PTP? How are you measuring that? Multimeter? Oscilloscope? What model of 'scope? Perhaps it is 5V PTP but your equipment is unable to measure it accurately?

1 Like

Hello @PaulRB ,

Very good point. The first thing I did after reading your reply was to measure the average DC Voltage at PB4 (with PB4 set as output) using a DMM (model Uni-T UT33A+), and indeed, it says 2.5V (the square wave has 50% duty cycle). So according to this, Peak to Peak must be 5V.

Then I again looked at my Oscope (yeapook handheld Oscilloscope 5012h .. literally got it from here).

This is what it shows.

Now I don't know what to believe.

best regards,
aquaman

Can you try some different prescaler settings to bring the frequency down and re-measure with DMM and 'scope?

1 Like

Hello @PaulRB ,
I tried various different prescalar settings and brought it down from 500KHz to about 5 KHz. The Oscope VPP (peak to peak voltage) remained at 1.2V . DMM's average voltage always showed 2.5V (not sure if there is a DC offset).
Though, the waveform on Oscope becomes very jittery beyond 50KHz. Not sure, this is how ATTiny85 behaves (since this is PWM mode after all, where we don't expect square waves with sharp edges.. so long as the average voltage is right, it will do).

Maybe, I will harass a colleague of mine and try to get ahold of Siglent or Rigol scope and see how it fares (this is a personal project, doing it in my garage). Any other ideas?

thanks,
aquaman

We don't?

What if you run the standard blink sketch on that pin, say at 50Hz (10ms HIGH, 10ms LOW)? Or 5Hz.

Good idea to try a more expensive 'scope.

1 Like

If it is 1.2V, a white led with a 330 ohm resistor will be dark.

1 Like

You should be using a DC as it is really DC, not AC, which your scope shows. That will also trim the square corners of the trace.

1 Like

Well I spent some time going over the data sheet and the code and it seems it should work. So.. I got out an Attiny85, my programmer (a bit of a problem since I haven't done this in a while) and a scope. Here is the result...


It works fine in my basement, ~500KHz and 5.44 p-p. It appears you have measurement problems or a bad device.

1 Like

Hello @oldcurmudgeon ,
You are absolutely right.
I rewired everything again. Now, it works with the same code in post #1. My Oscope shows 5V on PB4 at 500KHz.
Loads of thanks to you and to everyone else for helping to find resolution.
Marking your post as solution.

best regards,
aquaman

But what is that solution? Other forum members with a similar problem may find this topic and be frustrated that the original cause of the problem has not been positively identified. @oldcurmudgeon demonstrated, using the same circuit & code but a different 'scope, that it does give 5V PTP signal, but not why your readings were different. Was @Paul_KD7HB 's suggestion the real solution, in fact?

1 Like

Hello @PaulRB,
I changed my breadboard. I think there is some issue with the breadboard. I had previously only thought of changing components and wires, but after @oldcurmudgeon confirmed that code worked, I started from scratch. Changed all components, wires and breadboard. It worked. Then after few repeats, narrowed down the cause to be the breadboard.More specifically, the area on the breadboard where the ATTiny85 chip was placed. I had used this breadboard off and on only for testing leds, so didn't realize that some specific connection holes were not working ok.

What was happening was that the area where I had put the ATTiny85 was causing some weird coupling between ATTiny85 pins. It didn't damage the chip but the output on PB4 was not coming out right. And since I was using this board as programmer, I had marked the point where I put the ATTiny85 chip, so as to not have to change wiring when flipping different ATTiny85 chips for programming. So even other ATiny85 chips were giving the same issue. Really, Murphy's law in full glory.

Regarding @Paul_KD7HB 's suggestion, I appreciate the reply. Perhaps the photo in post #5 is bit blurry, but near top middle, its showing DC (in little blue square background). So, the scope was in DC mode.

best regards,
aquaman

1 Like

Thanks for the update, that clears things up for other forum members I hope.

There was one other outstanding question:

1 Like

Hello @PaulRB,
I was talking of pattern analyzer style digital output with sharp, 90 degree transition edges, absolutely 0 ringing. I think the ringing and jitter/noise I was seeing was a result of aforesaid coupling due to bad breadboard. As mentioned in post #14, since location of all different ATTiny85 chips was the same, I was suspecting that something was wrong either with code or that ATTiny chips behave this way. I don't see any issues now that breadboard has been changed. I do still see a little bit of ringing around waveform transition edges, but that is acceptable due to possibly issues in scope's waveform capture (like probes inducing stray inductance or capacitence, impedence matching etc.) as well as output pin driving characteristics of the chip.

Best regards,
aquaman

Yes, I suppose so, at frequencies approaching a megahertz, you are approaching the limits of breadboarding, even with good quality/condition breadboards. Plus, the probes that come with cheap 'scopes are going to be cheap probes...

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.