NOTE

Analog Output - Intro

void setup() {
  pinMode(9, OUTPUT);
}

void loop() {
  int onTime = analogRead(A0);
  digitalWrite(9, HIGH);
//microsecond
  delayMicroseconds(onTime);
  digitalWrite(9, LOW);
  delayMicroseconds(1023 - onTime);
}

Servo

#include<Servo.h>
Servo myServo;
void setup() {
  myServo.attach(9);
}

void loop() {
  int sensor = analogRead(A0);
  int angle = map(snesor, 0, 1023, 0, 180);
  if(millis() % 20 < 2){
    myServo.write(angle);    
  } 
  //delay(20);
}

Play Tones

void setup() {
  // nothing to do here
  Serial.begin(9600);
}
 
void loop() {
  int analogValue = analogRead(A0);
  Serial.println(analogValue);
  float frequency = map(analogValue, 0, 1000, 100, 1000);
  tone(2, frequency, 10);
  delay(10);
}

4f27a9328b005dfa7727d72756e51b86.mp4

When I let delay become 200, there's more rhythm.

13bc5a7683e984eb7f1ffbe1311292b7.mp4

Lab: Tone Output Using An Arduino

ee2a3676e347b1538838a9cbc598154.jpg

The sound does get louder when I used transistor

A more complex example

A Musical Instrument