# rectangular signal

#### cheniour

##### Member
Hello
in my project, I need to use an oscillator. and because of covid19 pandemic, I am no longer able to command the tools I need, I was obliged to use carte Arduino DUE

to extract rectangular signals of magnitude 1.5v , frequency 32.768 kHz

and duty ratio 0.5 I used this program

255 = 5v so 1.5v= 76.5 and T=0.0152587891 secondes

so I used this program and I don't know what's going wrong
int LED_pin =11; // Brancher votre oscilloscope avec pin = 11 sur la carte arduino

int level = 76.5; //PWM_out_level : cela signifie le rapport cyclique
void setup() {
pinMode(LED_pin, OUTPUT);

}
void loop() {
analogWrite(LED_pin, level);
delay(0.0152587891);
analogWrite(LED_pin,0);
delay(0.0152587891);

}

I think that the problem in the oscilloscope

cordially,

#### rjenkinsgb

##### Well-Known Member
From a quick look at the Arduino delay() instruction, it takes a long integer parameter, so only a whole number of milliseconds.

Code:
Parameters
ms: the number of milliseconds to pause (unsigned long)
Converted to an integer, the 0.015 is zero.

#### JimB

##### Super Moderator
255 = 5v so 1.5v= 76.5
int level = 76.5;
If you are defining the variable level as an integer, it cannot have the value 76.5 (not an integer).
Values of 76 or 77 are OK for an integer.

JimB

#### cheniour

##### Member
it's a correct code ?

#### Nigel Goodwin

##### Super Moderator
it's a correct code ?
Doesn't matter what it is - it's an incorrect value, as you can't use a decimal value for an integer variable - as already suggested use 76 or 77 instead for the level.

Also your delay (as also already pointed out) is in milliseconds, not seconds, and also needs to be an integer number - not a decimal one - so should be 15.

#### Musicmanager

##### Active Member
You may have another fundamental problem .. the Arduino DUE operates at 3.3v not 5v .. .. ..so the level calculation is only about 2/3rds of what it should be .. .. .

MM

#### cheniour

##### Member
so if I want 1.5v ? 77 fault value?
there is no problem with 32khz the Arduino due can operate correctly it supports this huge value?

#### Nigel Goodwin

##### Super Moderator
Nigel Goodwin need 32khz so in delay i should write 0.015ms
No - you can't have a decimal point. You need a delay of 62uS.

You should check out:

Although I suspect you would probably be better trying to use a hardware timer?.

#### cheniour

##### Member
no it's impossible in my case

#### Nigel Goodwin

##### Super Moderator
Nigel Goodwin t 1.5 V what I should write in my case?
delayMicroseconds(62);

However, I'm a bit bemused as to exactly what you're trying to do?.

If you're using PWM you don't need the delays, as you should set the PWM to the frequency you want.

If you not using PWM, then use the delays and simple digital writes to generate the signal.

#### cheniour

##### Member
I Need pwm with variable duty cycle also 1.5 v magnitude

#### Nigel Goodwin

##### Super Moderator
I Need pwm with variable duty cycle also 1.5 v magnitude
I don't think you understand what PWM is?, or what PWM magnitude is?.

PWM is just a square wave pulse output, with an amplitude of the supply rail (3.3V for a Due) - altering the duty cycle just alters the width of the pulses, the voltage remains at 3.3V. The 'analogue' output is no such thing, it's just a series of ON and OFF digital pulses that average out to whatever voltage you were looking for (so 50/50 duty cycle would average out to 1.65V).

#### cheniour

##### Member
Nigel Goodwin I try to work with this code and the resultat appear like this whatis the problem, please the amplitude equl 5.20V and I need 1.5v

Code:
int LED_pin = 11; // Brancher votre oscilloscope a

int level = 50;  //PWM_out_level : cela signifie le rapport cyclique comme il est illustré sur le photo
void setup()
{
pinMode(LED_pin, OUTPUT);
}
void loop()
{
analogWrite(LED_pin, level);
delayMicroseconds(50);
analogWrite(LED_pin, 0);
delayMicroseconds(50);
}

#### Nigel Goodwin

##### Super Moderator
I've already told you - that code won't work, you can't use analogueWrite, that simply gives a PWM signal, and doing it twice with time delays makes no sense.

Can you tell us EXACTLY what you're trying to do, and why it has to be 32KHz, and why it has to be 1.5V.

#### rjenkinsgb

##### Well-Known Member
I Need pwm with variable duty cycle also 1.5 v magnitude
You can only set an absolute voltage level if a device has a digital to analog converter (DAC) included.

I'm not familiar with the Arduino so did not catch that part - I thought at least you were using a DAC.

Apparently the "analog" output is a pseudo analog PWM signal in itself, so if you use that at all, you will get a pulsed signal and only be turning the pulses on and off (or changing the duty cycle) by writing different values.

That looks to be exactly what you see on the scope, a string of pulses when you write a non zero output value and no pulses when you write zero.

In other words the arduino can only give logic-level signals and the only way you can get a different voltage is by using external components - eg. a resistive divider or preset pot to set the signal level.

#### RobinSm89

##### New Member
Unfortunately, I cannot help you with this, as I myself am trying to find solutions for this problem. If suddenly you find out the answer to your question, I will also be grateful if you tell me

#### atferrari

##### Well-Known Member
You should forget for a moment, all what you did up to now.
Step back, breath deeply and explain what you need to solve.
You could be guided easily to a (most probably) simpler solution.

In the beginning I hated when Nigel asked that question but he was right in doing so.

Explain you problem not your "solution".

#### Dr_Doggy

##### Well-Known Member
pin is 5v and u need 1.5? add some external resistors.