Square wave Duty Cycle shifter - help

meth

Joined May 21, 2016
304
I might have not understood the problem completely, I thought to add a little more ON-time to increase the duty cycle, but that ON-time would be different for each frequency to achieve 50% duty cycle. My bad.
 

Ian0

Joined Aug 7, 2020
13,133
why not use frequency doubler followed by frequency divider? this will ALWAYS give you exactly 50% duty cycle.
See post #38, but it only operates within a limited range of frequencies. If the voltage on the capacitor reaches the supply it will no longer have a 50% duty cycle.
 

AnalogKid

Joined Aug 1, 2013
12,143
What I can’t understand is why something would not saturate at 50% duty-cycle but would at 30%. Sounds back-to-front to me.
Depends on which signal phase causes saturation in whatever is downstream. 30% high means 70% low. Something that is integrating during the low phase might not have enough time to recover during the high phase.

ak
 

AnalogKid

Joined Aug 1, 2013
12,143
I might have not understood the problem completely, I thought to add a little more ON-time to increase the duty cycle, but that ON-time would be different for each frequency to achieve 50% duty cycle. My bad.
Yeah, that is the problem. Adjusting a time period, and adjusting the *ratio* of two time periods, are two very different problems to solve.

Early on I thought this would need a feedback servo, a ramp, and a comparator of some kind. I was about half-way through it using a CD4093 rather than a 555, when carl posted his solution. Some audio power amplifiers use a similar method to remove DC errors from the output without using an input coupling capacitor.

ak
 

MisterBill2

Joined Jan 23, 2018
27,584
I'm a little disheartened.
Honestly I imagined it wouldn't be easy. At this point the only real solution would be with an ATtiny. I have to find someone to help me for the moment with code and then, as Jerry rightly advised, it's time for me to learn to do it too, but I imagine it won't be something immediate.
Anyway I have to compliment this forum and obviously its participants, I didn't imagine I would find so much participation and solidarity, thank you so much!
OK, here is a scheme to adjust the duty cycle in 1% increments, which will work unless that input frequency is changing quite rapidly:
First, a PLL/divider system produces a second frequency exactly 100 times the starting frequency. Next, at every rising edge of that starting frequency, the output FF is set. Then, when the counter advancing from that 100X frequency reaches a count of 50, the output FF is reset.
The primary limitation is the ability of that PLL/counter scheme to accurately follow the starting frequency x100. Probably the same scheme can be done in a small processor, but that would simply be a software implementation of the same scheme.
 

Jerry-Hat-Trick

Joined Aug 31, 2022
827
Just for fun I tried it with two Arduino UN0s. One to generate the signal with two pots to vary frequency and mark/space, the other to convert to 50/50 mark space. Code is:

int mark = 0;
int counter = 0;
int latest = 0;
int last = 0;

void setup() {
pinMode(7,INPUT);
pinMode(4,OUTPUT);
}

void loop() {
if(last == 0 and latest == 1){
mark = counter*2/3;
counter = 0;
digitalWrite(4,HIGH);
}
counter++;
last = latest;
latest = digitalRead(7);
if (counter>mark) digitalWrite(4,LOW);
}

Yellow is input, blue is output

1741533965805.jpeg
1741534005586.jpeg
1741534057451.jpeg

Whilst I think the 555 solution is really elegant, once you get used to processors it becomes quick and easy. This took me about two hours to lash together and write the code (which could do with some improvement but it works). As mentioned, before, the Digispark ATTiny85 would be a quick inexpensive solution
 

Thread Starter

ilgadget

Joined Mar 2, 2025
12
Just for fun I tried it with two Arduino UN0s. One to generate the signal with two pots to vary frequency and mark/space, the other to convert to 50/50 mark space. Code is:

int mark = 0;
int counter = 0;
int latest = 0;
int last = 0;

void setup() {
pinMode(7,INPUT);
pinMode(4,OUTPUT);
}

void loop() {
if(last == 0 and latest == 1){
mark = counter*2/3;
counter = 0;
digitalWrite(4,HIGH);
}
counter++;
last = latest;
latest = digitalRead(7);
if (counter>mark) digitalWrite(4,LOW);
}

Yellow is input, blue is output

View attachment 344123
View attachment 344124
View attachment 344125

Whilst I think the 555 solution is really elegant, once you get used to processors it becomes quick and easy. This took me about two hours to lash together and write the code (which could do with some improvement but it works). As mentioned, before, the Digispark ATTiny85 would be a quick inexpensive solution
Good afternoon dear Jerry,
forgive me, I am a total loser in Arduino programming. Would the program you posted be enough to program an ATtiny to do the job I want? I didn't dare ask for that much, but for me it would be great!
 

Jerry-Hat-Trick

Joined Aug 31, 2022
827
Would the program you posted be enough to program an ATtiny to do the job I want? I didn't dare ask for that much, but for me it would be great!
Yes, with small adjustments. I strongly recommend you use the Digispark ATtiny85 because it actually plugs directly into the USB socket on your PC and it has a voltage regulator on board. I’ll try it myself some time next week just to make sure
 

Thread Starter

ilgadget

Joined Mar 2, 2025
12
Yes, with small adjustments. I strongly recommend you use the Digispark ATtiny85 because it actually plugs directly into the USB socket on your PC and it has a voltage regulator on board. I’ll try it myself some time next week just to make sure

There are no words to express all my gratitude!!! I try... thank you with all my heart!
It was the solution for me. Interesting suggestions from other friends, but I have very little space to integrate the change and a micro, "in every sense" was the solution for me.
I look forward to hearing from you.
 
This script works on the ATtiny85 Digispark. Input into P2, Output from P3. Let us know how you get on. By the way, in theory ma the value of "mark" should be period/2 but for some reason it's not exact so adjusting it to mark = period * 3/5 gives a better result on my 'scope. You could adjust this to suit - the mark space appears to remain the same across the input frequency and mark/space range.

One thing to be aware of with the Digispark,, there is a 5 second delay before it starts to implement the code. When uploading a new program, you "upload" and quickly insert the Digispark into the USB port within those five seconds. If you are comfortable to use a bare 8 pin IC this won't happrn, but you'll need to buy or build a programmer.

int mark = 0;
int counter = 0;
int latest = 0;
int last = 0;

void setup() {
pinMode(2,INPUT);
pinMode(3,OUTPUT);

}

void loop() {
if(last == 0 and latest == 1){
mark = counter*3/5; // theoretically mark = counter/2
counter = 0;
digitalWrite(3,HIGH);
}
counter++;
last = latest;
latest = digitalRead(2);
if (counter>mark) digitalWrite(3,LOW);
}
 
Last edited:

panic mode

Joined Oct 10, 2011
5,002
small board or MCU like that is a perfect fit for the job.
i would just add that while Digispark is not an Arduino, it is programmed using same software and pretty much all instructions that apply to Arduino apply to Digispark. But while small size is great, there are some tradeoffs and notable one is that transferring file to Digispark is a bit different (requires plugging it in at right time). I suggest following tutorials on Digispark (like https://startingelectronics.org/tutorials/arduino/digispark/), and asking for help if you get stuck. I prefer to keep Digispark plugged to a USB cable and for transfer process, plug/unplug the other end of the cable.
Btw, you can connect potentiometer to one of the pins so you can conveniently adjust the duty cycle.
 

Thread Starter

ilgadget

Joined Mar 2, 2025
12
This script works on the ATtiny85 Digispark. Input into P2, Output from P3. Let us know how you get on. By the way, in theory ma the value of "mark" should be period/2 but for some reason it's not exact so adjusting it to mark = period * 3/5 gives a better result on my 'scope. You could adjust this to suit - the mark space appears to remain the same across the input frequency and mark/space range.

One thing to be aware of with the Digispark,, there is a 5 second delay before it starts to implement the code. When uploading a new program, you "upload" and quickly insert the Digispark into the USB port within those five seconds. If you are comfortable to use a bare 8 pin IC this won't happrn, but you'll need to buy or build a programmer.

int mark = 0;
int counter = 0;
int latest = 0;
int last = 0;

void setup() {
pinMode(2,INPUT);
pinMode(3,OUTPUT);

}

void loop() {
if(last == 0 and latest == 1){
mark = counter*3/5; // theoretically mark = counter/2
counter = 0;
digitalWrite(3,HIGH);
}
counter++;
last = latest;
latest = digitalRead(2);
if (counter>mark) digitalWrite(3,LOW);
}
Fantastic, I can't believe it!!! I hurry to order an ATtiny85 on Digispark pcb to try it as soon as possible, I can't wait. Then I think of programming an ATtiny via an Arduino uno as an ISP programmer since I will mount the micro itself into my little circuit.
I will definitely keep you updated on the result, in the meantime I send you my infinite thanks!!!
 

Ian0

Joined Aug 7, 2020
13,133
Fantastic, I can't believe it!!! I hurry to order an ATtiny85 on Digispark pcb to try it as soon as possible, I can't wait. Then I think of programming an ATtiny via an Arduino uno as an ISP programmer since I will mount the micro itself into my little circuit.
I will definitely keep you updated on the result, in the meantime I send you my infinite thanks!!!
You'll be back, when you find out that saturation doesn't depend on just the duty cycle. . .
 

sparky 1

Joined Nov 3, 2018
1,218
The MCP602 series rail to rail op amp Evaluation board demonstrated some useful PCB recommendations.
The duty cycle has presented some challenges, In general working with the sensitive gates of mosfets.
Simulation software like Qorvo Qspice are focusing on better ways to make it possible to use GaN and SiC.
Not just modules and datasheets but pcbs having simulation that integrates pcbs images including search engines.
The datasheet for some of the latest GaNs are linked to simulations as a much better way to show specifications.

There are simulations that detract from the 3D fields in PCBs by giving an impression that electrical circular balls run in pipes.
n-MOSFET

Working with efields, checking pcb designs with better instrumentation.
ELECTRICAL FIELD EVALUATION BOARD | Microchip Technology
 
Last edited:

LesJones

Joined Jan 8, 2017
4,511
I think a phase locked loop with the oscillator in the phase locked loop running at twice the input frequency might solve the problem. The divided (by 2) signal will have a 50% duty cycle. The phase comparitor could just use the the rising (Or falling .) edge of the input waveform together with the rising edge of the output of divide by 2 from the phase controlled oscillator.
Les.
 
Top