Arduino PWM Frequency Tweaked to 1kHz

แชร์
ฝัง
  • เผยแพร่เมื่อ 30 ก.ย. 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

ความคิดเห็น • 65

  • @daxliniere
    @daxliniere ปีที่แล้ว +4

    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;

  • @pev_
    @pev_ 2 ปีที่แล้ว +12

    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 ปีที่แล้ว +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 ปีที่แล้ว +1

      @@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.

  • @gsoundzvid
    @gsoundzvid 2 ปีที่แล้ว +2

    Julian, what is that scope you are using ?

  • @Ken-oe3ch
    @Ken-oe3ch 2 ปีที่แล้ว +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);
    }

  • @cmuller1441
    @cmuller1441 2 ปีที่แล้ว +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 |

  • @ohmedarick1
    @ohmedarick1 2 ปีที่แล้ว +1

    Hi great video. What brand of handheld oscilloscope is it that you are using? Thanks

  • @phils6926
    @phils6926 5 หลายเดือนก่อน

    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.

  • @kennedy67951
    @kennedy67951 2 ปีที่แล้ว +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 ปีที่แล้ว +3

      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...

  • @sjm4306
    @sjm4306 2 ปีที่แล้ว +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 ปีที่แล้ว +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).

  • @gavbansal967
    @gavbansal967 2 ปีที่แล้ว +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 ปีที่แล้ว +1

      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!

  • @TheEmbeddedHobbyist
    @TheEmbeddedHobbyist 2 ปีที่แล้ว +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 ปีที่แล้ว +1

      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

  • @andymouse
    @andymouse 2 ปีที่แล้ว +1

    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.

  • @spielautomat9751
    @spielautomat9751 2 ปีที่แล้ว +1

    try fast led outputs .. FastLED 3.x

  • @rustham_2845
    @rustham_2845 2 ปีที่แล้ว

    Can i get your Arduino coding?

  • @allinclusive5762
    @allinclusive5762 2 ปีที่แล้ว +1

    Very useful! Thanks 👍

  • @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.

  • @aardwolf21
    @aardwolf21 2 ปีที่แล้ว

    Isn't the default PWM frequency for an ESP8266 1kHz? Would save faffing.

  • @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" :)

  • @jack91522
    @jack91522 ปีที่แล้ว

    Is there a web page where you can enter any frequency and it'll tell you what combinational settings to use?

  • @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

  • @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

  • @diydsolar
    @diydsolar 2 ปีที่แล้ว

    Hi, I will check how much it changes with temperature. Just in case.

  • @paulmeynell8866
    @paulmeynell8866 ปีที่แล้ว

    That was a really interesting video thank you very much.

  • @christopherkise
    @christopherkise 2 ปีที่แล้ว

    Just map the travel to the range you want. Nice coding

  • @piconano
    @piconano 2 ปีที่แล้ว +1

    I like that little scope.

    • @JulianIlett
      @JulianIlett  2 ปีที่แล้ว +1

      I like it for video making. The bigger Keysight scope is harder to fit into the shot.

  • @shivam000
    @shivam000 2 ปีที่แล้ว

    What's the name of oscilloscope?

  • @GnuReligion
    @GnuReligion 2 ปีที่แล้ว

    Good to see you fiddling with the bits.

  • @marcusjenkins
    @marcusjenkins 2 ปีที่แล้ว

    This would be for your DIY EVSE, right?

  • @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 ปีที่แล้ว +1

      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.

  • @lezbriddon
    @lezbriddon 2 ปีที่แล้ว

    i hate the timer and interrupt registers....

    • @lucasc5622
      @lucasc5622 2 ปีที่แล้ว +1

      thats funny, because they dont speak highly of you either

  • @jlucasound
    @jlucasound 2 ปีที่แล้ว

    Hi Julian!!

  • @noggin73
    @noggin73 2 ปีที่แล้ว +1

    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 ปีที่แล้ว +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/

  • @BeezyKing99
    @BeezyKing99 2 ปีที่แล้ว +1

    Audruino is getting boring... get on with that diesel heater you sold out for.... otherwise I'll take it.

    • @JulianIlett
      @JulianIlett  2 ปีที่แล้ว +1

      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)