Analog Output: Pulse width modulation (PWM) vs. frequency modulation
Why we need analog output? It is clearly that in real life, we don’t want to control everything in just two states: on and off. We do adjusting all the time: dimming the light, volume up the radio and etc. Here is analog output for.
Arduino cannot output varying voltage while it can fake it with PWM. Thanks to this lab, I have a chance to brush up on all terminology of PWM as well as its function. From my understanding, the average voltage of a a series of voltage pulse should have a jaw shape. Its voltage increase gradually while at the peak, it drops. But I think the drop here is not like a cliff drop from 5v to 0v while it still has a slight slop to buffer. (I am not 100% sure if I make this right?)
I have to perform the lab in week3 with an Arduino UNO board because my Arduino Nano iot33 was broken after I accidentally spilled my coffee over it.
LED dimming
In Arduino, analogWrite() is used to change the brightness of an LED.
Here I am not sure about the “map” function, but with the help from Yeseul, I understood that this function is to convert large range to a small one.
Servo control
#include <Servo.h>
Servo servoMotor;
int servoPin = 3;
void setup() {
Serial.begin(9600);
servoMotor.attach(servoPin);
}
void loop() {
// read the input on analog pin 0:
int analogValue = analogRead(A0);
// print out the value you read:
Serial.println(analogValue);
int servoAngle = map(analogValue, 0, 1023, 0, 179);
servoMotor.write(servoAngle);
}
(technical issue, working on uploading videos)
Project 1 is at the baby stage.