arduino IDE 1.x, 2.x
Board Manager : Arduino UNO R4 Boards 1.0.2
File -> example -> UNO R4 xxx example -> AnalogWave -> SineWave
#include "analogWave.h"
analogWave wave(DAC);
int freq = 10; // in hertz, change accordingly
void setup() {
Serial.begin(115200);
pinMode(A0, INPUT);
wave.sine(freq);
}
void loop() {
freq = map(analogRead(A0), 0, 1024, 0, 10000);
Serial.println("Frequency is now " + String(freq) + " hz");
wave.freq(freq);
delay(1000);
}
This example code outputs the signal received with A0 to DAC.
But A0 is a DAC pin.
Web page examples use A5 instead of A0.
the same example
https://docs.arduino.cc/tutorials/uno-r4-minima/dac
The example seems to be invalid.
Thanks for bringing this to our attention @nyk_yeon . A fix for the example is now in progress:
arduino:main
← jacobhylen:jacobhylen/dac-analogwave-fix
opened 12:43PM - 18 Jul 23 UTC
SineWave example of the AnalogWave library was using A0 as an input pin, but the… A0 pin is the DAC output pin.
https://forum.arduino.cc/t/invalid-r4-sinewave-ino-example/1149139
This PR changes the potentiometer input pin to A5, to match the other DAC examples in the library.
2 Likes
Just change A0 to A5 and it will work! It appears twice in the code, you need to change both instances.
I came up with this simple fix and it worked.
/*
* Generate a sin wave with frequency
* determined with a pot on A1
* Wave output on A0
*/
#include "analogWave.h"
analogWave wave(DAC);
int freq = 1000; // in hertz, change accordingly
void setup() {
Serial.begin(115200);
pinMode(A1, INPUT);
wave.sine(freq);
}
void loop() {
freq = map(analogRead(A1), 0, 1024, 0, 10000);
Serial.println("Frequency is now " + String(freq) + " hz");
wave.freq(freq);
delay(1000);
}
Hello everybody !
to generate a frequency i use this code from 5Hz to 10kHz
*
* Generate a sin wave with frequency
* Wave output on A0
*/
#include "analogWave.h"
analogWave wave(DAC);
int freq = 2000; // in hertz, change accordingly
void setup() {
Serial.begin(115200);
wave.sine(freq);
}
void loop() {
Serial.println("Frequency is now " + String(freq) + " hz");
wave.freq(freq);
delay(1000);
}