To browser these website, it's necessary to store cookies on your computer.
The cookies contain no personal information, they are required for program control.
  the storage of cookies while browsing this website, on Login and Register.

Storing Cookies (See : ) help us to bring you our services at . If you use this website and our services you declare yourself okay with using cookies .More Infos here:
If you do not agree with storing cookies, please LEAVE this website now. From the 25th of May 2018, every existing user has to accept the GDPR agreement at first login. If a user is unwilling to accept the GDPR, he should email us and request to erase his account. Many thanks for your understanding

User Menu

Custom Search

Author Topic: Arduino code for pulsing H-Bridge at exact frequencies  (Read 884 times)

Offline floodrod

  • Hero Member
  • *****
  • Posts: 722
    • Mooker.Com- Energy Discovery Forums
Arduino code for pulsing H-Bridge at exact frequencies
« on: September 09, 2022, 07:43:09 PM »
I just wanted to post this in case it helps others..

Using arduino and H-bridge to flip polarities, it can be difficult to pulse DC square waves at exact controllable frequencies without complex timers, etc.  And counting delay microseconds is hit or miss..

I figured out a way to use the "Tone Function" to control frequency of pulses.  Just have to change the Hz value in the code and it's dead on..  I been playing with a resonance circuit and the frequency on the scope is exact..  Be aware- it will only work up to 31 Khz tho..

!!!!!!!!!!!!!!!!!!!!!!!!!!  CODE !!!!!!!!!!!!!!!!!!!!!!!

int r1 = 6;
int r2 = 7;
int receiver = 8;

void setup(){

   pinMode(r1, OUTPUT);
   pinMode(r2, OUTPUT);
   pinMode(receiver, INPUT);
   tone(5, 1000);


void loop() {
if (receiver == HIGH)
  digitalWrite(r1, LOW);
   digitalWrite(r2, HIGH);
digitalWrite(r1, HIGH);
digitalWrite(r2, LOW);



Now on the arduino, put a jumper from D5 to D8.   And connect your H bridge PWM inputs to pins 6 and 7.  Thats it....
To change frequency-  find this code "tone(5, 1000);"   and change 1000 to whatever hertz you want up to 31000 Hz.

Maybe someone will find this useful!  Easier than messing with 555 timers..