Week 4: Analog Out with Arduino

In this lab I controlled a servo motor with a variable resistor, in this case a 5K potentiometer.

First, the setup: I powered the breadboard with 5V and connected it to ground (GND). Not shown in the pictures is my laptop, which powers my Arduino, except when I'm adding or changing components on the breadboard.

Then, I connected my pot's red wire to power and black wire to GND. The middle, blue wire of the pot connects to Analog Pin 0 on the Arduino board. I soldered this pot to wires because I noticed in Tuesday's class that directly attaching a pot to the breadboard lacked stability and gave jumpy values. 

Next, I ran a short sketch in the Arduino Web Editor to check the input range of values for my pot when turning the wiper. It returned 0-1023.

After that, I connected my servo motor: brown wire to GND, red wire to power, and the yellow wire to Digital Pin 9. This type of servo moves its arm 180 degrees, so I wrote a new script in the Web Editor to map the values of the pot to the angle of the servo (0-179). I marked the ends of the attached blades to improve visibility.  

Observation: I noticed that when not touching the pot, I could still hear the servo motor, even though the arm was still. It sounded fidgety, like the motor was trying to move. 

Curious, I added a 4.7K Ohm resistor in series with the 5K pot. Without changing my Arduino program, the servo arm moved 90 degrees. Why was that? Well, I ran a quick input sensor reading on the pot, and the values returned changed to 493-1023, close to half of the original range. Half of 180 degrees is 90. It was easy enough to remap these new pot values (493-1023) to the servo (0-179) for the full range of motion. Instead, I could alway adjust the servo range to dial in a particular angle swing, but this was a fun little experiment!

 

Tone Control

Following the tone output lab, I attached a photocell to Analog Pin 0 with a 10K pulldown resistor to GND and an 8-Ohm speaker to Digital Pin 8. By attaching a library of notes to my Arduino sketch, I programmed the tiny tune in the example.

170929_MG_0513.jpg

In the last exercise I connected three photocells to Analog Pins 0, 1, and 2, and moved the speaker to Digital Pin 13. Next, I gathered the photocells' input values: topSensor 400-860, middleSensor 540-880, bottomSensor 80-840. Slightly modifying the sketch from the lab, I rewrote it such that if any sensor reading registered below a threshold of 600, a note played. Click to hear my limited keyboard above. I cupped my hand around the speaker for a cleaner sound than the examples above. It's still annoying, though.