Hi Friends
I am using a arduino uno board to Generate a pwm and also program a DAC. The pwm generated by the arduino board is not the expected signal.
Expected PWM signal : high - 34 ms low - 4 microseconds
Generated PWM signal is : high 34ms , low - 600 us .
I am pasting the code below for reference.
#include <Wire.h> // Include wire library for I2C communication
#include <TimerOne.h>
void setup()
{
Serial.begin(9600);
Wire.begin();
pinMode(9, OUTPUT);
pinMode(A4,INPUT_PULLUP);
pinMode(A5,INPUT_PULLUP);
}
void loop()
{
pwm();
byte busStatus;
do
{
Wire.beginTransmission(0x1C); //checking the presence of DAC
busStatus = Wire.endTransmission();
}
while (busStatus !=0x00); //checking that DAc has aaccepted its address and has sent ACk signal
Serial.println(busStatus); //Serial Monitor should show 0
//-------------------------------
Wire.beginTransmission(0x1C); //address of DAC 0x1C
Wire.write(0x08); //pointing user configurable regsiter
Wire.write(0x00);
Wire.write(0x00);
Wire.endTransmission();
Wire.beginTransmission(0x1C); //address of command byte register/load code and load to DAC
Wire.write(0x01);
Wire.write(0x5D); //upper 8-bit data for DAC
Wire.write(0xC0); //lower 8-bit data for DAC
Wire.endTransmission();
}
void pwm ()
{
digitalWrite(9, LOW);
delayMicroseconds(4);
digitalWrite(9, HIGH);
delay(34); // Approximately 10% duty cycle @ 1KHz
}
as @Delta_G has said you cant use delay to manage times when you are also running other code.so presently you have
void pwm ()
{
digitalWrite(9, LOW); //time signal is LOW
delayMicroseconds(4);
digitalWrite(9, HIGH); Make signal high
delay(34); // Approximately 10% duty cycle @ 1KHz
}
.. add more time so signal is high for unknown duration and unknown duty cycle
also 4 microseconds : 34 milliseconds is nowehere near 10%.
Do you NEED a "1kHz" PWM or could you use the 980Hz the Uno natively provides with an Analog Write?
Generate a PWM by manually switching pin on and off in the loop - a bad idea at all. But combine it with configuration of DAC and printing to the Serial making the goal of producing correct PWM almost unreachable.
Why did you programmed a DAC in the loop? Do you need program a DAC every 34 ms?
It correspond to the duty of 0.01%.
Can you tell us in more detail why you need a PWM with such a small duty? In technical terms, such a sequence is usually called not PWM, but just a single 4us pulse with a interval of approximately 34ms
With what accuracy should these parameters be maintained? For example, if the impulse is not 4us, but 3.125, will this be a problem?
A single 3.125us pulse at 34ms intervals can be programmed using a hardware timer, so it will work independently of the main sketch code.
#include <Wire.h> // Include wire library for I2C communication
void setup()
{
Serial.begin(9600);
Wire.begin();
pinMode(9, OUTPUT);
pinMode(A4,INPUT_PULLUP);
pinMode(A5,INPUT_PULLUP);
Wire.beginTransmission(0x1C); //address of DAC 0x1C
Wire.write(0x08); //pointing user configurable regsiter
Wire.write(0x00);
Wire.write(0x00);
Wire.endTransmission();
Wire.beginTransmission(0x1C); //address of command byte register/load code and load to DAC
Wire.write(0x01);
Wire.write(0x5D); //upper 8-bit data for DAC
Wire.write(0xC0); //lower 8-bit data for DAC
Wire.endTransmission();
}
void loop()
{
pwm();
byte busStatus;
do
{
Wire.beginTransmission(0x1C); //checking the presence of DAC
busStatus = Wire.endTransmission();
}
while (busStatus !=0x00); //checking that DAc has aaccepted its address and has sent ACk signal
Serial.println(busStatus); //Serial Monitor should show 0
//-------------------------------
}
void pwm ()
{
digitalWrite(9, HIGH);
delayMicroseconds(10); // Approximately 10% duty cycle @ 1KHz
digitalWrite(9, LOW);
delay(34);
}
I did an experiment with an Uno once to see how long a digital write takes... I don't remember the details but I kept a note of the results -
When I wrote high or low, and then the opposite with no delay I got a 3.3uS pulse. (Maybe you can confirm that.) If you add 4uS to that, you should get 7 or 8uS so I don't know where the 600uS is coming from... Or maybe my experiment was wrong and a write takes longer?
After the 30mS delay you are doing other things that will also take some time before you come-back through the loop again.