Objective: To explore analog input and output by reading sensor values (from variable resistors) to a serial monitor and utilizing pulse width modulation (PWM) for output
Variable Input and Output: For this part of the lab, I built a circuit using a photocell, a radial potentiometer, and 2 LEDs. The photocell and radial potentiometer served as analog sensors while the LEDs served as analog outputs. Connecting the potentiometer to the breadboard felt very similar to the Neopixel strip in the Digital I/O lab. One wire went to power, another wire went to a pin on the Arduino, and the last wire went to ground. The major difference was that I had to make sure to use an analog pin, so I chose pin A6 according to the Arduino Every diagram. Setting up the photocell (variable resistor) felt similar to setting up a button, but I was actually building a voltage divider circuit. One wire connected to power, another wire connected to pin A3, and a 10K ohm resistor connected to ground.
Each of my LEDs connected to a 100 ohm resistor, which then connected to different pins on the Arduino. My yellow LED connected to pin 5, and my red LED connected to pin 3. On the Arduino Every diagram, I had to make sure to choose PWM pins, which were labeled with a “~” next to the pin number.
Schematic for variable input and output circuit
Breadboard setup for variable input and output circuit
I coded the Arduino such that if the radial potentiometer was rotated, pin A6 would receive an analog signal. The Arduino’s analog to digital converter (ADC) would convert the signal into an input value from 0 to 1023. Then through pulse width modulation (PWM), pin 5 would send a series of voltage pulses to turn on/off the yellow LED based on that value. Generally speaking, these voltage pulses are so fast that the human eye does not see blinking. When the value is higher, the LED is on for longer than it is off, which makes it appear brighter. This way, the potentiometer would act as a dimmer for the yellow LED. Since analog outputs have values from 0 to 255, I had to divide the input value by 4.
Regarding the photocell, pin A3 would receive an analog signal depending on light intensity. Ideally, the ADC would also convert the signal into an input value from 0 to 1023, but this usually doesn’t happen in practice. When I checked the range with the serial monitor, my values were from 80 to 440. This time, pin 3 would send voltage pulses to turn on/off the red LED based on the input value. Therefore, the photocell would act as a dimmer for the red LED. Since my range of values for the photocell didn’t map as easily as the range of values for the potentiometer, I had to use a function called map().
https://gist.github.com/tranjames267/485a6e2d0b586212aa427a1d57529660
A video showing my variable input and output circuit working
Sensor Box: For my sensor box, I wanted to make a music box where a photocell and a potentiometer would be the sensors, and LEDs and a Piezo buzzer would provide output. I drew a sketch of what I wanted my box to look like. I had to make sure to consider the dimensions of the box so that I could fit my breadboard inside with some extra room for my components.
Sketch of my sensor box
Next, I needed to laser cut the pieces of my box. I created a laser cutting file and then laser cut some cardboard. I had to remember that red lines would instruct the laser to cut through the cardboard while black lines would instruct the laser to make an etch. To assemble my box, I used both duct tape and packing tape. For the photocell (light sensor) hole, I covered it with a clear film of packing tape to allow light to pass through easily. For the light feedback holes, I covered them with translucent scotch tape to diffuse the light from the LEDs that would eventually be inside the box. I was pretty satisfied with the result.
Constructed sensor box (front)
Constructed sensor box (back)
With my box constructed, I moved on to my circuit. The schematic was very similar to the first part of the lab since I added a Piezo buzzer to pin 5 and moved one of the LEDs to pin 4. Otherwise, the rest was pretty much the same. In terms of the resistor, I tested out a 100 ohm resistor, but the noises the Piezo buzzer made were still very piercing and loud. After using a 10K ohm resistor, the noises were quieter and generally more tolerable.
Schematic of the circuit inside my box
Of course, I had to consider user interactions for my sensor box. I couldn’t just have my radial potentiometer and photocell directly plugged into my breadboard. Therefore, I made a breakout board for my photocell so that it could reach the top of my box underneath the light sensor hole. I also soldered solid core wires directly onto the potentiometer so that it could stick out from the side of my box and still plug into the breadboard. While I could have just used the Piezo buzzer as it was, the stranded wires that came with it did not plug into the breadboard well. To fix this problem, I soldered headers to these wires just to make the construction process easier for my circuit.
Internal circuitry for the sensor box
Since I used solid core wire, fitting my circuit inside my box was a little difficult. It was harder to bend the wires, but the wires would keep their shape after I bent them into a position I wanted. Luckily, my box opened from the top, so I could gradually add components inside and secure them in place with packing tape. For my potentiometer in particular, adding the knob outside the box helped it stay put.
Circuitry inside the sensor box
In terms of my code, I found Piezo buzzer notes/code for a song called “Zelda’s Lullaby” by Robson Couto. The notes and note durations were arranged in an array, and the code would loop through the array to play the song. I added code such that the tempo of the song would change based on the input value provided by the radial potentiometer. Additionally, the sensor box would shine a white LED when the photocell sensed higher levels of light and shine a blue LED when the photocell sensed lower levels of light.
https://gist.github.com/tranjames267/59ed0b7c05b1941b60f468c6d5813d32
A video showing my sensor box working (video uses sound)