Hello, I am new to Arduino and I currently picked up the Arduino Zero. I am trying to take a 3 volt peak to peak (0 to 3 volts) sine wave input and replicate the sine wave on the output using PWM. The input signal is 1 to 10 kHz. I have attached my code below. I use analogRead to obtain a sample from pin A0, which I call val. I then use analogWrite the write a mapped value to the pwmPin to create the pwm generated signal. However, the PWM is not corresponding to the input. I am lost as to why this isn't working. If anyone could point me in the right direction it would be appreciated.
Thanks
int pwmPin = 6;
int inPin = A0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
int val = analogRead(inPin); // read the input pin
analogWriteResolution(8);
analogWrite(pwmPin, map(val,0,1023,0,255));
}
First of all be careful with the 3 V input: considering the max. voltage you can give the pins before damage is 3.3 V, you are very near the limit.
Here below is an excerpt of Zero's description. As you can see, your sketch uses A0 as input, while it would be much better to use it as a DAC output, as specified. Then you can use any other of the Analogue pins (A1 to A5) as inputs.
DAC: A0. Provide a 10bit voltage output with the analogWrite() function.
PWM: 3, 4, 5, 6, 8, 9, 10, 11, 12, 13. Provide 8-bit PWM output with the analogWrite() function.
SPI: SS, MOSI, MISO, SCK. Located on the ICSP header only support SPI communication using the SPI library.
LED: 13. There is a built-in LED driven by digital pin 13. When the pin is HIGH value, the LED is on, when the pin is LOW, it's off.
Analog Inputs. Six of the 20 general purpose I/O pins on the Zero provide analog input. These are labeled A0 through A5, and each provide up to 12bits of resolution (i.e. 4096 different values). By default they measure from ground to 3.3 volts, though is it possible to change the upper end of their range using the AREF pin and the analogReference() function.