Arduino PWM Frequency Tweaked to 1kHz

  Рет қаралды 15,073

Julian Ilett

Julian Ilett

2 жыл бұрын

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/$/invite/@julian256:d
Octopus Energy referral link: share.octopus.energy/aqua-birch-918
(We each get £50)
#EasyEVSE
#ElectronicsCreators

Пікірлер: 65
@daxliniere
@daxliniere Жыл бұрын
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;
@GnuReligion
@GnuReligion 2 жыл бұрын
Good to see you fiddling with the bits.
@pev_
@pev_ 2 жыл бұрын
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.
@IanSlothieRolfe
@IanSlothieRolfe 2 жыл бұрын
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.
@stuartmcconnachie
@stuartmcconnachie 2 жыл бұрын
… 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.
@pev_
@pev_ 2 жыл бұрын
@@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.
@TheOnlyEru
@TheOnlyEru 2 жыл бұрын
@@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.
@pev_
@pev_ 2 жыл бұрын
@@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.
@allinclusive5762
@allinclusive5762 2 жыл бұрын
Very useful! Thanks 👍
@davidv1289
@davidv1289 2 жыл бұрын
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
@piconano
@piconano 2 жыл бұрын
I like that little scope.
@JulianIlett
@JulianIlett 2 жыл бұрын
I like it for video making. The bigger Keysight scope is harder to fit into the shot.
@phils6926
@phils6926 2 ай бұрын
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.
@gavbansal967
@gavbansal967 2 жыл бұрын
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.
@JulianIlett
@JulianIlett 2 жыл бұрын
Cool, thanks. Next step is to drive the CP line (+/- 12V) from the 5v Arduino output. I'm thinking opto-isolators :)
@gavbansal967
@gavbansal967 2 жыл бұрын
@@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!
@Hoptronics
@Hoptronics Жыл бұрын
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.
@paulmeynell8866
@paulmeynell8866 Жыл бұрын
That was a really interesting video thank you very much.
@andymouse
@andymouse 2 жыл бұрын
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.
@JulianIlett
@JulianIlett 2 жыл бұрын
Cheers :)
@gsoundzvid
@gsoundzvid 2 жыл бұрын
Julian, what is that scope you are using ?
@ohmedarick1
@ohmedarick1 2 жыл бұрын
Hi great video. What brand of handheld oscilloscope is it that you are using? Thanks
@diydsolar
@diydsolar 2 жыл бұрын
Hi, I will check how much it changes with temperature. Just in case.
@alisoncarney5889
@alisoncarney5889 2 жыл бұрын
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
@Ken-oe3ch
@Ken-oe3ch 2 жыл бұрын
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); }
@jack91522
@jack91522 Жыл бұрын
Is there a web page where you can enter any frequency and it'll tell you what combinational settings to use?
@sjm4306
@sjm4306 2 жыл бұрын
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.
@JulianIlett
@JulianIlett 2 жыл бұрын
Outputs 5 & 6, which use timer1 can have 16-bit PWM resolution
@sjm4306
@sjm4306 2 жыл бұрын
@@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).
@shivam000
@shivam000 2 жыл бұрын
What's the name of oscilloscope?
@dentakuweb
@dentakuweb 2 жыл бұрын
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" :)
@spielautomat9751
@spielautomat9751 2 жыл бұрын
try fast led outputs .. FastLED 3.x
@kennedy67951
@kennedy67951 2 жыл бұрын
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.
@JulianIlett
@JulianIlett 2 жыл бұрын
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.
@gsoundzvid
@gsoundzvid 2 жыл бұрын
@@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...
@marcusjenkins
@marcusjenkins 2 жыл бұрын
This would be for your DIY EVSE, right?
@JulianIlett
@JulianIlett 2 жыл бұрын
Yes :)
@jlucasound
@jlucasound 2 жыл бұрын
Hi Julian!!
@JulianIlett
@JulianIlett 2 жыл бұрын
Hej!
@TheEmbeddedHobbyist
@TheEmbeddedHobbyist 2 жыл бұрын
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.
@Handlebrake2
@Handlebrake2 2 жыл бұрын
Lol, no one ever reads the manual 😜
@TheEmbeddedHobbyist
@TheEmbeddedHobbyist 2 жыл бұрын
@@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. 🙂
@Handlebrake2
@Handlebrake2 2 жыл бұрын
@@TheEmbeddedHobbyist we're lucky to have ppl like you
@piconano
@piconano 2 жыл бұрын
I'm surprised someone hasn't created a library, to output any valid frequency the hardware PWM can generate!
@JulianIlett
@JulianIlett 2 жыл бұрын
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.
@piconano
@piconano 2 жыл бұрын
@@JulianIlett Coming from the PIC world, I use the peripherals only at register level.
@aardwolf21
@aardwolf21 2 жыл бұрын
Isn't the default PWM frequency for an ESP8266 1kHz? Would save faffing.
@cmuller1441
@cmuller1441 2 жыл бұрын
9:20 This look like those stupid questions all over the internet about 2+3*4 or something similar... Operator precedence!!!! & is done before |
@noggin73
@noggin73 2 жыл бұрын
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()!
@JulianIlett
@JulianIlett 2 жыл бұрын
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/
@lezbriddon
@lezbriddon 2 жыл бұрын
i hate the timer and interrupt registers....
@lucasc5622
@lucasc5622 2 жыл бұрын
thats funny, because they dont speak highly of you either
@rustham_2845
@rustham_2845 Жыл бұрын
Can i get your Arduino coding?
@christopherkise
@christopherkise 2 жыл бұрын
Just map the travel to the range you want. Nice coding
@BeezyKing99
@BeezyKing99 2 жыл бұрын
Audruino is getting boring... get on with that diesel heater you sold out for.... otherwise I'll take it.
@JulianIlett
@JulianIlett 2 жыл бұрын
The diesel heater broke, then went rusty.
@BeezyKing99
@BeezyKing99 2 жыл бұрын
@@JulianIlett wouldn't have gone rusty/corroded if you hadn't neglected it.
@JulianIlett
@JulianIlett 2 жыл бұрын
It's still broken.
@BeezyKing99
@BeezyKing99 2 жыл бұрын
@@JulianIlett need any pointers on how to fix them, check out David Mcluckie (not sure if that's correctly spelled)
Arduino Basics Change your PWM Frequency
7:35
learnelectronics
Рет қаралды 87 М.
map(), constrain() and solar stuff
18:27
Julian Ilett
Рет қаралды 6 М.
Tom & Jerry !! 😂😂
00:59
Tibo InShape
Рет қаралды 67 МЛН
KINDNESS ALWAYS COME BACK
00:59
dednahype
Рет қаралды 120 МЛН
Arduino Basics 101: Hardware Overview, Fundamental Code Commands
8:02
Make a Really Simple Square Wave Generator
9:28
Blueprint
Рет қаралды 72 М.
Pulse Width Modulation (PWM) - Electronics Basics 23
7:28
Simply Electronics
Рет қаралды 647 М.
How To Create A PWM Signal Using The Arduino Uno - For Beginners
15:15
OPA Ep5 - Arduino 1MHz Fast PWM Output Compare Registers
19:47
Dominic Does DIY
Рет қаралды 603
Arduino Motor Interfacing using MOSFET and PWM
10:49
Dorian McIntire
Рет қаралды 187 М.
Electronic Basics #30: Microcontroller (Arduino) Timers
9:19
GreatScott!
Рет қаралды 435 М.
Gizli Apple Watch Özelliği😱
0:14
Safak Novruz
Рет қаралды 7 МЛН
1$ vs 500$ ВИРТУАЛЬНАЯ РЕАЛЬНОСТЬ !
23:20
GoldenBurst
Рет қаралды 1,6 МЛН