Low frequency periodic signal with duty cycle regulation

Hello,
In order to command a pneumatic electro-valve, I need to generate a periodic signal with a frequency of 3 to 10 Hertz, and I need to have a duty cycle depending on a fixed "high" time (which I can get trough calculation and regulate with a potentiometer)
so basically I need to generate a periodic signal of 3 to 10Hz with a modifiable duty cycle.
My problem is that PWM can't generate such low frequencies, and with classic timers code, I have a fixed duty cycle of 50 % ....
As I'm quite new to coding, don't know the right keywords for the fonctions I need, so if there's already a tutorial about this, just give me the link.

Many thanks in advance for your help

Hi,

With such a low frequency signal you can use even "delay" to get it.

Does the previous advice make any sense to you? (just to have an idea of what your skills are).

Best regards.

Hello, thanks for your advice, I already have a working code with delay(), but I have to use sensors while the periodic signal is emitted, so delays are quite annoying to do this…

Alexandre_Gilardot:
so basically I need to generate a periodic signal of 3 to 10Hz with a modifiable duty cycle.
My problem is that PWM can't generate such low frequencies, and with classic timers code, I have a fixed duty cycle of 50 % ....

Any thoughts about the timing resolution?
Is one millisecond enough?
Do you need a smaller timing resolution?

Let's say, 1 millisecond is OK, then you could do that:

  • Use a timer to fire 1000 interrupts per second
  • Define your cycle duration (100 with 10Hz, 333 with 3 Hz)
  • Calculate duty cycle in milliseconds
  • Do some calculations and output setting in the interrrupt handling

So if you want 10 Hz and 30% duty cycle, you let your interrupt handling

  • count up
  • if counter at 100 ==> new cycle begins, set counter=0, set output high
  • if counter at 30 (30% of 100) ==> set output low

And if you want 3 Hz and 20% duty cycle, you let your interrupt handling

  • count up
  • if counter at 333 ==> new cycle begins, set counter=0, set output high
  • if counter at 67 (20% of 333) ==> set output low

If timer resolution should be better than 1 ms, this would be possible: Timer interrupts also can run much faster than 1000 times per second.

What do you think? Which is the required timer resolution / duty cycle accuracy of your application?

Considering the switching time of the electro-valve, 1ms is totally ok.
Your solution seems able to do the job, but I have difficulties to code it, my knowledge about timers being a bit too short.
If you could give me a bit of hints, that would be very kind.

Alexandre_Gilardot:
Considering the switching time of the electro-valve, 1ms is totally ok.
Your solution seems able to do the job, but I have difficulties to code it, my knowledge about timers being a bit too short.
If you could give me a bit of hints, that would be very kind.

Coding timers is hardware dependent.
So you may need different code with different Arduino boards.

For which type of Arduino board do you need it? UNO (Atmega328) or something else?

Additional question: Are there already timers in use, explicit or implicit, such like timers used for PWM, servo control or something else, or do you start the sketch from scratch?

Hi,

Just using the millis() function you will have time to poll hundreds of sensors (i.e: for a "10 Hz and 30% duty cycle" you have 0,3 s to do as many things as you can before changing the output).

Best regards

Hi,
I will use an arduino uno, starting from scratch.
I'm building an airsoft replica running on a paintball tank.
Here's quite everything the arduino will have to do :
Read the position of fire selector and then detect trigger, then :
-if on single : cycle one and only one time till trigger is released
-if on burst : cycle a determined number of time (using map() and a potentiometer on analog input) till trigger is released

  • if on full : cycle as long as the trigger push button is "high"
    -read from a potentiometer to determine cycle frequency
    -read from a potentiometer to determine cycle duty
    -light 2 IR leds when "chrony" button is on
    -detect low and high state of 2 photoresistors (using map() and constrain()) to calculate bb speed
    -read from a potentiometer to adjust speed

An LCD screen 16x2 will be attached to the arduino and shall show :

  • firing mode/number of shots for burst
  • shots per second
  • cycle duty converted in milliseconds of "high" time
  • bb speed calculated on the basis of time between falling of photoresistor 1 and falling of photoresistor 2

So i think I need a timer for the cycling and a timer for bb speed

You can use millis() for everything else, but the speed measurement is too fine. You would have to use counters/interrupts for that. Also, photoresistors may be too slow to respond to a speeding bullet. You may have to use photodiodes or phototransistors.

It's an extremely complex task for your first device, first program.

Hi,
I will use an arduino uno, starting from scratch.
I'm building an airsoft replica running on a paintball tank.
Here's quite everything the arduino will have to do :
Read the position of fire selector and then detect trigger, then :
-if on single : cycle one and only one time till trigger is released
-if on burst : cycle a determined number of time (using map() and a potentiometer on analog input) till trigger is released

  • if on full : cycle as long as the trigger push button is "high"
    -read from a potentiometer to determine cycle frequency
    -read from a potentiometer to determine cycle duty
    -light 2 IR leds when "chrony" button is on
    -detect low and high state of 2 photoresistors (using map() and constrain()) to calculate bb speed
    -read from a potentiometer to adjust speed

An LCD screen 16x2 will be attached to the arduino and shall show :

  • firing mode/number of shots for burst
  • shots per second
  • cycle duty converted in milliseconds of "high" time
  • bb speed calculated on the basis of time between falling of photoresistor 1 and falling of photoresistor 2

So i think I need a timer for the cycling and a timer for bb speed

Yes, it looks too complicated even for an experimented programmer. (by the way: what is an "airsoft replica"?; and a "paintball tank"?)

Best regards

vffgaston:
Yes, it looks too complicated even for an experimented programmer. (by the way: what is an "airsoft replica"?; and a "paintball tank"?)

Best regards

I'm not so sure, look completely doable, and if you break it into chunks you'll do just fine. The only slightly more involved part is the timer interrupt to drive the valve, but jurs has already thrown out a solution for that (response #3)

At first I won't use the bb speed mesure, it's only for the final project.
Driving the valve with firing mode and regulators, with LCD display would be totally fine at first.
An airsoft replica is a toy gun shooting 6mm biodegradable plastic bullets with a muzzle energy of 2joules or less, used in similar games as paintball, and a paintball tank is an aluminium bottle in which pressurized air is stored at an approximative pressure of 3500psi.

I'm not so sure, look completely doable, and if you break it into chunks you'll do just fine. The only slightly more involved part is the timer interrupt to drive the valve, but jurs has already thrown out a solution for that (response #3)

Yes. I added the "too" when rereading the post: It is "simply" complicated, but doable (and a nice project, I would say).

Best regards