The Arduino and other digital microcontrollers generally can’t produce a varying voltage, they can only produce a high voltage or low voltage. Instead, you “fake” an analog voltage by producing a series of voltage pulses at regular intervals, and varying the width of the pulses. This is called pulse width modulation (PWM)
analogWrite(pin, duty);
A basic low-pass filter consists of a resistor and a capacitor.
*frequency = 1/ (2π resistance * capacitance)
https://itp.nyu.edu/physcomp/labs/labs-arduino-digital-and-analog/servo-motor-control-with-an-arduino/
https://itp.nyu.edu/physcomp/labs/labs-arduino-digital-and-analog/tone-output-using-an-arduino/
Question: sometimes it come out with:
Sketch uses 11908 bytes (4%) of program storage space. Maximum is 262144 bytes. Global variables use 3568 bytes (10%) of dynamic memory, leaving 29200 bytes for local variables. Maximum is 32768 bytes. No device found on cu.usbmodem2101 Failed uploading: uploading error: exit status 1
Question: this didn’t work
void setup() {
// nothing to do here
Serial.begin(9600);
}
void loop() {
// get a sensor reading:
int sensorReading = analogRead(A0);
// map the results from the sensor reading's range
// to the desired pitch range:
float frequency = map(sensorReading, 200, 900, 0, 1000);
// change the pitch, play for 10 ms:
tone(8, frequency, 10);
delay(10);
Serial.println(sensorReading);
}void setup() {
// nothing to do here
Serial.begin(9600);
}
void loop() {
// get a sensor reading:
int sensorReading = analogRead(A0);
// map the results from the sensor reading's range
// to the desired pitch range:
float frequency = map(sensorReading, 200, 900, 0, 1000);
// change the pitch, play for 10 ms:
tone(8, frequency, 10);
delay(10);
Serial.println(sensorReading);
}
Solved: map reversed.