Arduino PWM Frequency Tweaked to 1kHz
HTML-код
- Опубликовано: 27 авг 2024
- New User Get $30 User Coupons jlcpcb.com/CYT
Other Users Get SMT Coupons via Contacting JLCPCB Facebook
The Arduino Uno, Nano and Pro Mini all have PWM frequencies of 490Hz (on outputs 3, 11, 8 and 9) and 976Hz (on outputs 5 and 6). In this video I tweak the frequency from 490Hz to exactly 1kHz.
Subscribed to my 2nd channel? Watch all my 3 minute videos on Julian's Shorts: / @juliansshorts
Interested in my new garden workshop? Follow the entire build on Julian's Shednanigans:
/ @juliansshednanigans
Join me on Odysee/LBRY: lbry.tv/$/invi...
Octopus Energy referral link: share.octopus.energy/aqua-birch-918
(We each get £50)
#EasyEVSE
#ElectronicsCreators
F8 = 248 for those who haven't looked it up yet. ;)
Thanks for this video, it made setting PWM frequency a piece of cake. Perhaps you could put the code into the video description? I had to pause and copy it in.
//Set PWM pin to 1kHz
TCCR2B = TCCR2B & 0b11110000 | 0b00001011;
OCRA2 = 0xF8;
When you do bit manipulation like you do to TCCR2B here, you should always first zero (needed) bits with AND, and then OR to set needed bits, and do these operations in separate sentences, that way there is no question about operator priority! Just a simple habit that is good to pick up and sustain in order to avoid bugs like you did at first.
Or you can use parenthesis around the AND. That will ensure that TCCR2B only gets written to once, which isn't important in this example but may be in other contexts.
… and always bracket AND and OR (and other logic operations in the same expression). No one but a programming pendent remembers operator precedence over and above than the traditional BODMAS.
@@IanSlothieRolfe Unless you know exactly how the specific compiler handles things, parentheses are still no guarantee of that, and they make the operation look even more obscure. And there are a dozen more different contexts that may require extra things to be taken into consideration. Sorry, I stand by my words, the most clear and most bug-avoiding way in this context is as I said. With added comments to explain what is being done, of course.
@@pev_ heck no. If the compiler can’t be trusted to respect parentheses you need to switch compiler.
And I would never approve code during a code review that splits up simple operations into multiple operations. I can accept some extra parentheses to make logic safer and easier to understand.
Above four-five operands or so is a different thing, then you probably need to split it up to make the logic clear.
@@TheOnlyEru First, I did not say a compiler will not respect parentheses!! Second, if you are in a position to "approve code" and you say splitting operations into simpler ones is a strict no-no, then goodbye, I have no respect for your "reasoning". And your last sentence is such idiotic "I just make up this figure to rule everything" that I do not even want to try to respond.
Somehow I stumbled up on this video..
15+ years ago, maybe 20.. I bought these atmel chips and Crystal's and all the stuff to make a dev board, no arduino bootloaders just straight som then c. I made a parallel programmer then a usb one and so on.. stopped for awhile...
Now a year back into it I was so confused about what happened to all the low level stuff. This brings me right back to the datasheets.. lol.. thank you for this.
After a lot of faffing around, I eventually got this to run.
I do tinker with Arduino code, but not at register level.
I did get it to run at 980 Hz or thereabouts, but nothing I did would get it to budge,
I can only assume that somewhere in the copying code from the screen and cutting and pasting the register values, something got messed up.
With nothing to lose but an old Nano, I set TCCR2B to B00000000, followed by setting TCCR2B to B00001011.
No ANDING or ORING, but it did work and I got near enough 1.004-kHz.
As Julian suggested later on, it's not a bad idea to read what's in a register before you change it by simply adding a line or two Serial.print(TCCR2B).
Also thrown by the Atmel document describing the bits that are changed as Read Only.
However, pleased to get there eventually.
Good to see you fiddling with the bits.
Thanks for all your vids. Like you I'm developing an EVSE. To generate the 1khz PWM I'm using an ESP8266, very straightforward to generate the 1khz PWM, and it's also Arduino IDE and IOT compatible.
Cool, thanks. Next step is to drive the CP line (+/- 12V) from the 5v Arduino output. I'm thinking opto-isolators :)
@@JulianIlett
Ooh that'll be interesting! Look forward to seeing you're approach. Optoisolator for level switching I take it?
I'm looking into the safety stuff, PEN fault detection and GCFI, looks fairly straightforward but researching suitable current transformers....and then there's also tying it to my solar PV, relay fault detection...and the list goes on! Good fun though!
I like that little scope.
I like it for video making. The bigger Keysight scope is harder to fit into the shot.
Interesting video. I used the same technique to generate a 1 MHz clock for a MC68488 chip connected to an ATmega. I am always amazed by the versatility of these chips - - hence the huge datasheets. Regards, David
Hi great video. What brand of handheld oscilloscope is it that you are using? Thanks
Julian, what is that scope you are using ?
Very useful! Thanks 👍
That was a really interesting video thank you very much.
Julian is that scope really any good? I know your using it in your video, so it must be decent. Right? I'm thinking of buying it. Would you recommend it? I hope I don't come off sounding stupid mate. I just don't want to waste my money. Thanks for the upload.
It suits me because it's small and fits on my desk (and in the video frame). But it does have a low resolution screen so it's not super accurate.
@@JulianIlett Pretty useless reply, Julian, surely since James was asking about the scope it would have been a complete reply to include some detail, unless of course You-tube forbids such content, I too would like to know as per my posted request...
I've never really thought about this before and I think I will try this at home because I wonder how fast rise time and stuff like that is...cheers.
Cheers :)
Did you try using the tone() command? I am reading 998.4Hz and 499Hz from this sketch...
void setup() {
}
void loop() {
tone(9, 1000);
delay(1000);
tone(9, 500);
delay(1000);
}
It's always boggled my mind a bit why the default (in fact only) pwm frequency of the analogWrite function is so low frequency (~300Hz). The 328p can easily go up to ~62kHz (at the loss of some resolution), and easily past 1kHz while having plenty of resolution.
Outputs 5 & 6, which use timer1 can have 16-bit PWM resolution
@@JulianIlett depends on which pwm mode is used, and the max count value (which directly affects frequency). IIRC using the 16 bit timer, min timer prescale, and outputting max ~62kHz will only net you 7 bit resolution. It's a hardware limitation without having a higher base clock frequency or pll to boost the timer input clock. Requiring 16 bit pwm resolution would greatly decrease output frequency (base clock is 16MHz, assuming 1x prescale at 16 bit res would mean max freq is ~244Hz if my math is correct).
The problem is with the Arduino, no one reads the chip specifications to learn what it can do and how to do it. It's all in the data sheets. there will be a few registers which control the PWM and you should be able to set the frequency to whatever you like.
Lol, no one ever reads the manual 😜
@@Handlebrake2 Well i have to admit that i'm one of the sad ones who do ;-) the "Absolute Maximum Ratings" are a must how else will you realy know how to kill it. 🙂
@@TheEmbeddedHobbyist we're lucky to have ppl like you
Hi, I will check how much it changes with temperature. Just in case.
There's something about setting registers and ANDing and ORing that I like way more than traditional Arduino coding.
I'm not a software person but this kind of stuff just feels closer to the hardware and I somehow undestand it better.
By the way, if you needed more granularity you can set the PWM resolution to more than 8 bits but that's not needed for what an EVSE does.
It feels weird saying AN EVSE because you wouldn't say "an equipment" :)
hi julian why did you not use the video code in your tutorial pwm 2 would this no work for what you are doing and if not explain but very help full yet again
try fast led outputs .. FastLED 3.x
Is there a web page where you can enter any frequency and it'll tell you what combinational settings to use?
Hi Julian!!
Hej!
Nice one. I've been missing these kind of videos!
I'm following along with a Pro-Mini I had to hand. OCR2A = 0xF9 gets closer to 1kHz on my Rigol scope (exact, in fact).
Grrr. You didn't set the pin input / output in setup()!
It doesn't surprise me that 0xF9 is better. My little scope seems a bit inaccurate.
"You do not need to call pinMode() to set the pin as an output before calling analogWrite()."
It says so here: www.arduino.cc/reference/en/language/functions/analog-io/analogwrite/
9:20 This look like those stupid questions all over the internet about 2+3*4 or something similar...
Operator precedence!!!!
& is done before |
Isn't the default PWM frequency for an ESP8266 1kHz? Would save faffing.
What's the name of oscilloscope?
I'm surprised someone hasn't created a library, to output any valid frequency the hardware PWM can generate!
I used a library to do this a few years ago. Then the IDE changed and the library would no longer compile. Now I prefer to do these things myself at register level.
@@JulianIlett Coming from the PIC world, I use the peripherals only at register level.
This would be for your DIY EVSE, right?
Yes :)
i hate the timer and interrupt registers....
thats funny, because they dont speak highly of you either
Can i get your Arduino coding?
Just map the travel to the range you want. Nice coding
Audruino is getting boring... get on with that diesel heater you sold out for.... otherwise I'll take it.
The diesel heater broke, then went rusty.
@@JulianIlett wouldn't have gone rusty/corroded if you hadn't neglected it.
It's still broken.
@@JulianIlett need any pointers on how to fix them, check out David Mcluckie (not sure if that's correctly spelled)