Notifications
Clear all

Uno pin 8 digitalWrite output not symmetrical

12 Posts
3 Users
7 Likes
328 Views
(@wanaride67)
Member
Joined: 5 months ago
Posts: 5
Topic starter  

Hello!  I'm attempting Paul McWhorter's tutorial 26 that combines photoresistor and buzzer.  The idea; compute delay time, turn on buzzer for delay time, then turn off for delay time, both using digitalWrites.  I completed the first buzzer tutorial with digitalWrite, and observed on and off times were symmetrical (which made sense).  However, for some reason digitalWrites aren't symmetrical in tut 26.  For example, delay time is calculated as 10ms.  I noticed the buzzer was clicking, so I used my oscilloscope and observed on time = 10ms but off time = 36ms.  Any idea why?


   
Quote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6979
 

@wanaride67 If we could see the code the answer is likely obvious.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6979
 

@wanaride67 I tracked down the code and see that the 'off' time involves more code than the 'on' time thus explaining the difference.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Inst-Tech
(@inst-tech)
Member
Joined: 2 years ago
Posts: 554
 

Posted by: @wanaride67

Hello!  I'm attempting Paul McWhorter's tutorial 26 that combines photoresistor and buzzer.  The idea; compute delay time, turn on buzzer for delay time, then turn off for delay time, both using digitalWrites.  I completed the first buzzer tutorial with digitalWrite, and observed on and off times were symmetrical (which made sense).  However, for some reason digitalWrites aren't symmetrical in tut 26.  For example, delay time is calculated as 10ms.  I noticed the buzzer was clicking, so I used my oscilloscope and observed on time = 10ms but off time = 36ms.  Any idea why?

Hi @wanaride67, I just was reading your post regarding Paul McWhorter's tutorial#26.

If I'm understanding your question about the observed on and off time of the digital pin, and I'm assuming your using the same pins as Paul does), then the answer is, the digital pin your using is not a PWM pin, so in effect, you bit-banging the pin ( using a delay time to turn it off and on). This has the effect of the duty cycle being less than 50% when the frequency is over 400hz.

I ran the test n om Uno using the sketch from Paul's tutorial and noticed when the values of delayT were <4, the duty cycle started to drop..See this link: https://docs.arduino.cc/tutorials/generic/secrets-of-arduino-pwm

void setup() {
pinMode(13, OUTPUT);
}
 
void loop()
{
digitalWrite(13, HIGH);
delayMicroseconds(100); // Approximately 10% duty cycle @ 1KHz
digitalWrite(13, LOW);
delayMicroseconds(1000 - 100);
}

see this quote from the article.. Bit-Banging "This technique has the advantage that it can use any digital output pin. In addition, you have full control the duty cycle and frequency. One major disadvantage is that any interrupts will affect the timing, which can cause considerable jitter unless you disable interrupts. A second disadvantage is you can't leave the output running while the processor does something else. Finally, it's difficult to determine the appropriate constants for a particular duty cycle and frequency unless you either carefully count cycles, or tweak the values while watching an oscilloscope."

Hope this helps you with your understanding of how timers and PWM work in relation to bit-banging..

Regards,

LouisR

 

 

LouisR


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6979
 

@inst-tech @wanaride67 Also, have a look at ledcWrite and the supporting code. The nerds have an article and the 'deep' stuff is at https://espressif-docs.readthedocs-hosted.com/projects/arduino-esp32/en/latest/api/ledc.html . This is Espressif; I don't know if Arduino has a similar capability.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
Inst-Tech reacted
ReplyQuote
Inst-Tech
(@inst-tech)
Member
Joined: 2 years ago
Posts: 554
 

@zander , Hi Ron and Happy new year to you...

I did look at that article you posted and it looks very interesting , but it appears to be for espressif ESP32 SoCs, and @wanaride67 is using an Arduino Uno, digital pin 8.. which is not a PWM pin, so I don't think this would apply to him..but I'm bookmarking that site for further use later on when I finally get an ESP32-S3.. it's on my todo list..lol I've got quite a collection now of processor boards.

Thanks for the tip..

regards,

LouisR

 

LouisR


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6979
 

@inst-tech Agreed. I managed to get a couple of the S3 and C3 boards from Ali, but I made a booboo on one set so only have the smaller SRAM and FLASH, the other 3 or 4 are the BIG one's. No idea what I will do with all that power and space, but if/when I do I got the boards. Yes they took forever to get here but now safely in my parts drawers.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
DaveE and Inst-Tech reacted
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6979
 

@inst-tech Although I do have and use NANOs and UNOs plus a couple others from time to time, I do prefer the ESP32 family and forget how much less powerful the Arduino hobbyist family is. 

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
DaveE and Inst-Tech reacted
ReplyQuote
Inst-Tech
(@inst-tech)
Member
Joined: 2 years ago
Posts: 554
 

@zander Indeed, The Uno's are less powerful, but for the first timers and noobes.. it a very good start .. less intimidating because they are much simpler and less powerful..lol

I do alot of my software testing using Tinkercad because they do have the UNO and a editor to try out simple experiments without having to use actual parts..proof of concept. It's a lot quicker and I don't have to worry about letting the magic smoke out!.. 😆  With the built-in oscilloscope and a graphical trending feature, it's a very good tool for simple projects like what @wanaride67 is doing.

Take care my friend,

LouisR

LouisR


   
DaveE and Ron reacted
ReplyQuote
(@wanaride67)
Member
Joined: 5 months ago
Posts: 5
Topic starter  

@inst-tech Thank you!  I think you identified the issue, but I'm still somewhat confused.  Here is my understanding, please correct as necessary.

To make the buzzer "buzz", we need to provide it a signal with voltage that varies with time (aka frequency).  The only real possibility from an Arduino is a digital output with 50% duty cycle.

In Paul McWhorter's tutorial 26 the buzzer was supplied with a signal from Arduino pin 8 (digital pin).  Everything would have been OK so long as the duty cycle was 50%.

But according to your text, any attempt to switch pin 8 output between high/low at rate greater than 400Hz (delay time < 2.5ms) causes the duty cycle to fall below 50%, meaning "high" is on for less time than "low".  I did observe that in the tutorial, since most of the desired buzzer frequencies were > 400Hz.

HOWEVER...today I performed a test where I wrote different values to "delaytime", from 100ms to 100us (changed between "delay" and "delayMicroseconds") and observed duty cycle on my scope:

int testPin=8;
int delayTime=100;

voidsetup(){
  // put your setup code here, to run once:
pinMode(testPin, OUTPUT);
}

voidloop(){
  // put your main code here, to run repeatedly:
digitalWrite(testPin, HIGH);
delayMicroseconds(delayTime);
digitalWrite(testPin, LOW);
delayMicroseconds(delayTime);
}
 
At no point did I see a duty cycle less than 50%.  What am I missing?  Is it because today's test had pin 8 unloaded?

   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6979
 

@wanaride67 @inst-tech Have you looked at the Arduino library AVR-PWM?

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Inst-Tech
(@inst-tech)
Member
Joined: 2 years ago
Posts: 554
 

@wanaride67 , Yes, It could be as all, my test were with pin 8 loaded with a buzzer..try testing with the buzzer and see what happens..

@zander, Yes, I have looked at the PWM, but since were bit-banging a digital pin, I think it may not be relevant in this case..although I have been wrong on occasion.. 🤨 

But I'll check it out again..thanks Ron!

regards,

LouisR

LouisR


   
ReplyQuote