Some Questions in Mega2560.(sine or triangluar wave)

Here are some questions about modulation.

My project is wireless power transfer.
In this project, I use GDU, H-bridge(using D2PAK MOSFET), Coil, Current sensor, Rectifier circuit, and Arduino Mega2560.
And no more add external devices.

This is the current my code.

const uint8_t pinPhase_A = 6; //OC4A
const uint8_t pinPhase_B = 11; //OC1A
const uint8_t pinInvert_A = 7; //OC4B
const uint8_t pinInvert_B = 12; //OC1B
int Percent = 0;
int Duty = 0;
int TotalDuty = 80;
int Deadtime = 0;
int DutyInvert = 0;

void setup() {
  Percent = 50;
  Duty = Percent * TotalDuty/100;
  Deadtime = 10;
  DutyInvert = Duty + Deadtime;

  pinMode(pinPhase_A, OUTPUT);
  pinMode(pinPhase_B, OUTPUT);
  pinMode(pinInvert_A, OUTPUT);
  pinMode(pinInvert_B, OUTPUT);

  if(Duty >= TotalDuty-Deadtime || DutyInvert >= TotalDuty + Deadtime)
   Duty = TotalDuty;
   DutyInvert = TotalDuty;

if(Duty <= 0)
   Duty = 0;
   DutyInvert = 0;


  TCCR1A = _BV(COM1A1) | _BV(COM1B1) | _BV(COM1B0); //non-inverting A inverting B
  TCCR1B = _BV(WGM13) | _BV(CS10);//mode 8

  ICR1 = 80; 
  OCR1A = Duty;
  OCR1B = DutyInvert;

  TCCR4A = _BV(COM4A1) | _BV(COM4B1) | _BV(COM4B0); //non-inverting A inverting B
  TCCR4B = _BV(WGM43) | _BV(CS40); //mode 8

  ICR4 = 80;
  OCR4A = Duty; 
  OCR4B = DutyInvert;

  GTCCR = 0;


void loop() {
  // put your main code here, to run repeatedly:
 int sensorValue = analogRead(A0);

 float voltage = sensorValue * (5.0 / 1023.0);



A0 is used to read the voltage that enters the current sensor.
To transmit power, use 100 kHz.
A dead time to make sine wave.
Arduino mode is number 8, which is PWM, Phase and Frequency Correct.

Q1. Is it 100kHz sine wave?

 #include "math.h"
double Tsamp = 0.000001; 
double theta = 0; 
double f = 100000; 
double V_a = 0; 
double V_b = 0; 

void setup() 
void loop() 
  theta += 2*M_PI*f*Tsamp; 
  V_a = 5 * sin(theta); 
  V_b = -5 * sin(theta); 

I tried to check the sine wave with a serial monitor, but I don't know if this is really a 100kHz sine wave.(Serial monitor is not suitable for checking high frequency wave.)

And what's the range of theta that I need to set up?
it's heading toward infinity.

Q2. Is it okay to use triangular waves in Arduino?

I understand that triangular waves exist within Arduino to make pwm.
But I want to know "if these triangular waves are correct" and "if the method I'm trying to use is possible".

The triangular wave that I think is like the next picture.

TCNTn is triangular wave.
Compare this waveform to a sine wave.
If it is larger than sine waves, it reduces duty,
and if it is smaller, it grows duty.

In order to perform the above action, the frequency of TCNTn(tri wave) is 1000 kHz, which is greater than 100 kHz, but it would be nice to have a different size.

And isn't it possible to compare only when the amplitude is around 5V?

In summary, is TCNTn the triangular wave I'm looking for?
I want to know if i can adjust the amplitude and frequency of this waveform. (And the method)

(V_R is Q1 sine wave, V_T is Q2 triangular wave.

Thank you for reading the long questions.

I am not good at English, so there is an awkwardness in the question.

I know that answering all the questions is annoying and difficult.

So I'm grateful for all the small answers.

  • If there's anything else I need to explain, please write it down.

I would not try to generate a shaped (sine...) wave with an 8 bit 16MHz Arduino. What if you use the raw PWM rectangle? Eventually with a RC low pass filter?

The Mega can only produce rectangles. What you see as triangle in the diagram is the up/down count of the timer.

What's the intended outcome at the secondary side? What's the purpose of the current sensor?

In a first approach you can create a much lower frequency wave whose shape you can inspect or plot (Serial Plotter). Then increment the frequency until problems occur. Don't connect the coil for such tests.

The Mega can only produce rectangles.

I knew that.
When the pulse wave duty is adjusted through sinusoidal modulation, sine waves be generated after passing through H-bridge.

What you see as triangle in the diagram is the up/down count of the timer.

Then there are no triangle waves?
From what I learned in college, I heard that triangular waves are needed to make pulse waves in MCU.

What's the intended outcome at the secondary side?


The load(is 24W, 12V, 2A or others) is connected to the secondary side.
The feasibility was verified by other experts.

What's the purpose of the current sensor?

Measure the amount of current in the primary transmitter.
The first role is to stop the pwm operation in case of overcurrent.
The second role is for comparison with the command sine wave.(but not important)

For first role, the envelope detector is connected after the current sensor.

I thought it was possible to compare sine waves and triangular waves within Arduino without additional circuit connections, but is it impossible?

WGM 8 is phase and frequency correct PWM with TOP in ICRx. Setting TOP to 80 gives you 81 ticks per half cycle or 98,765.432 Hz. Did you mean to use TOP = 79 to get 100 kHz?

Not physical waves, only register values (TCNTn in your diagram).

that's not accurate

That's not accurate but realistic. The final wave shape is affected by the load.

so he's describing approximate waveform/voltages?

What's your point?

over a fraction of a waveform cycle, PWM can be used to approximate the voltage/power, but the harmonic content (distortion) of the waveform is very high.

it's not clear if the OP is simply interested in approximating a waveform

I prefer the time domain view. There a pulse results in exponential increase of the output voltage or current, depending on capacitive or inductive load. With a transformer and LED or light bulb as the load the resulting wave forms are almost unpredictable, but what matters is the effective power transmitted to the secondary circuit.

Oh, that's my fault.
I revised it as you said, and it's perfect.

If it looks like a triangular wave, it will be available like a triangular wave.
Is there a way to calculate the amplitude and frequency of this?

I know that due to the characteristics of Arduino currently used, the more severe the distortion of the waveform is.
I should have known this in advance, but I thought it was vaguely possible.
An appropriate device was configured to solve this problem on the receiving side.
I haven't done the experiment yet, but I don't think there will be a problem operating the load.

Yeah, that's what i want. :sob:

The TCNTn counter counts from 0 to TOP, that's the amplitude you provide. You adjust the timer clock by setting the prescaler. You also set the timer mode, whether it counts only up or up and down. Then mode, clock frequency and TOP give the frequency of the timer overflow event.

does the waveform really matter? if not, why not simply PWM?

We don't know yet, we'll know more when the project is finished.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.