What is PWM?
PWM is how a digital pin can simulate analog output: this can be useful for setting an LED to something in the middle of off and full brightness
See the slide below:
We can use PWM to do several things, but in this project we’ll use it to control a servo proportional to an ultrasonic sensor’s readings.
This is a CHALLENGE project, so you’ll be doing a majority of the coding and wiring for this project! We’ll give you a couple of hints and tips, but ultimately, executing the project is all up to you and your comprehension of the previous lessons.
So what is the task?
-
We want you to design code that rotates a servo proportionally to the distance your hand is from an ultrasonic sensor. Refer to the code in the previous lesson for the ultrasonic sensor.
-
We want you to do some of your own research on the Servo library that arduino provides, which is basically code that is already made which you can use to control a servo motor.
-
Servo library reference: https://www.arduino.cc/reference/en/libraries/servo/
-
Hint, you will need to use the
map()
function to convert the distance from the sensor to a rotational input to the servo, refer to this