element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Data Conversion
  • Challenges & Projects
  • Project14
  • Data Conversion
  • More
  • Cancel
Data Conversion
Blog Micro Servo Tester #3 : Now With Pulse Generator! (Video now working)
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Data Conversion to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: dubbie
  • Date Created: 15 Mar 2021 1:51 PM Date Created
  • Views 1919 views
  • Likes 9 likes
  • Comments 5 comments
  • pulse generator
  • micro servo tester
  • oled display
  • dataconverch
Related
Recommended

Micro Servo Tester #3 : Now With Pulse Generator! (Video now working)

dubbie
dubbie
15 Mar 2021

I have now written the code to produce pulses  to implement the pulse generator mode of the Micro Servo Tester, see the listing below. I have only included the pulse generatong function as that is the only part that has changed.

 

void pulsegeneratormode(void)

 

{

 

int frequency, pulsebit;

long int fdelay;

 

frequency = 1;

pulsebit = 1;

fdelay = 0;

 

  display.clearDisplay();  // Clear the display so we can refresh

  display.setCursor(0,10);  // Middle left

  display.println("Pulse Generator ");

  display.setCursor(0,30);  // Middle left

  display.println("Frequency ");

  display.setCursor(0,40);  // Middle left

  display.println("Pulse Width ");

  display.display();  // This outputs to the screen

  frequency = analogRead(adcfreq);  //Get the initial frequency value

  if (frequency < 1)

    frequency = 2;

  fdelay = (1000000 / frequency)/2;

 

  while (!buttonpress(button))

    {

      digitalWrite(pulseop, HIGH);

      if (fdelay < 10000)              // Pulse delay part

        {

          delayMicroseconds(fdelay);      

        }

      else

        {

          delay(fdelay/1000);

          delayMicroseconds(fdelay % 10000);

        } /* for */

      digitalWrite(pulseop, LOW);

      if (fdelay < 10000)               // Space delay part

        {

          delayMicroseconds(fdelay);      

        }

      else

        {

          delay(fdelay/1000);

          delayMicroseconds(fdelay % 10000);

        } /* for */

     

      frequency = analogRead(adcfreq);  //Get the frequency value

      if (frequency < 1)

        frequency = 2;

      fdelay = (1000000 / frequency)/2;

 

      pulsebit = analogRead(adcpulse);  //Get the pulse width value

//      mydisplaypulse(frequency, pulsebit);

    } /* while */

}

 

It is a relatively simple function as all it does is read the 10 turn pot controlling the frequency and then converts that into a microsecond delay. It does that by dividing 1 million by the measured frequency. So if the frequency is 1 Hz then the delay will be 1000000/1 = 1000000 us which is correct. I then divide this by 2 to produce the pulse part of the delay, and the space part will be the same. At present I have not implemented the capability to vary the pulse width so this will be an even mark to space ration pulse for all pulses.

 

The Arduino documentation indicates the the delayMicrosecond() function is not that accurate after 16,000 values so I used 10,000 as the cutoff. If any delay is greater than 10,000 us then it is converted into a number of millisecond delays, with a remainder (obtained using modulos) in microseconds. It seems to work. There will be some jitter or variation in frequency as the 10 turn pot is turned due to different length paths through this function, but the variation will be small.

 

A bigger problem is that the OLED display takes 37 milliseconds to update which is much too long for this use. So, I have deleted that part from the code. Sadly, this means that the OLED display is no longer used when in pulse generator mode. See the video below.

 

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

 

 

This problem also means that when in Servo Tester mode that the pulse being generated will be much to long. It should be a varying pulse part between 1 - 2 miiliseconds, followed by the 18 millisecond space delay, but with the display taking 37 ms to update, it means the space delay has increased to 53 milliseconds. It is a testament to the flexibility of the servo motors that they work at all!. I will have to have a rethink about all the functions of this system. Ah well, something to do on a rainy day.

 

Dubbie

  • Sign in to reply

Top Comments

  • beacon_dave
    beacon_dave over 4 years ago +4
    You may be able to use some of the AVR timer architecture to offload some of the timing work allowing you to use the display. You can set up a timer compare match interrupt to directly toggle an output…
  • dougw
    dougw over 4 years ago +3
    It sounds like a good application for a dual core MCU like the Pi Pico or arduino Nano RP2040.
  • ralphjy
    ralphjy over 4 years ago +3
    Seems like video encoding is having problems again. You should try it again or put a link to YouTube. After a couple of tries encoding failed yesterday, I just linked to YouTube and it worked fine. If…
  • dubbie
    dubbie over 4 years ago in reply to beacon_dave

    Dave,

     

    I did wonder about trying to use some of the AVR hardware to achieve both a pulse and a display but so far that's all I have done - wonder. I was side-tracked by 'wondering' how much jitter a pulse generator should or could have and so far I do not have any answer.

     

    Dubbie

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • beacon_dave
    beacon_dave over 4 years ago

    You may be able to use some of the AVR timer architecture to offload some of the timing work allowing you to use the display.

     

    You can set up a timer compare match interrupt to directly toggle an output pin, so your 10 turn pot would load the value in the output compare register and the rest is taken care of in the background, potentially leaving you time to update the display.

     

    Arduino uses some of the AVR timers for its own functions so you would need to be mindful of that.

     

    More information here which may be of interest:

    http://ww1.microchip.com/downloads/en/AppNotes/Atmel-2505-Setup-and-Use-of-AVR-Timers_ApplicationNote_AVR130.pdf

     

    Compare Match

     

    In cases where it is not sufficient to monitor a timer overflow, the compare match interrupt can be used.

     

     

    The Output Compare Register (OCRx) can be loaded with a value [0 .. MaxVal] which the TCNTn will be compared against in every timer cycle. When the timer reaches the compare value, the corresponding Output Compare Flag (OCFx) in the TIFRn register is set. The Timer can be configured to clear the count register to zero on a compare match.

     

     

    Related output pins can be configured to be set, cleared, or toggled automatically on a compare match.

     

     

    This feature is very useful to generate square wave signals of different frequencies. It offers a wide range of possibilities, which makes it possible to implement a DAC. The PWM mode is a special mode which is even better suited for wave generation. See the device datasheet for details.

     

    • Cancel
    • Vote Up +4 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • dubbie
    dubbie over 4 years ago in reply to ralphjy

    Ralph,

     

    I tried re-uploading the video but it didn't work so I took your advice, made it into a YouTube video and it worked straight away.

     

    Dubbie

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • ralphjy
    ralphjy over 4 years ago

    Seems like video encoding is having problems again.  You should try it again or put a link to YouTube.  After a couple of tries encoding failed yesterday, I just linked to YouTube and it worked fine.

     

    If you like ESP32s, that is another inexpensive option for dual core and you get WiFi.

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • dougw
    dougw over 4 years ago

    It sounds like a good application for a dual core MCU like the Pi Pico or arduino Nano RP2040.

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube