Lab 2: Controlling colors, sound and servos

In this second lab you will gain experience with color RGB leds, photoresistors, piezoelectric speakers, and servo motors using the Arduino development environment for writing programs. You can use a physical Arduino Uno for this lab or you can make use of an online simulation environment called tinkercad.

Part 1 - Control the color of an RGB LED

In this part you will create a color RGB led whose color can be changed through three potiontiometers. Before peforming the steps listed below you can also watch this video to see how the color RGB led can be used together with the Arduino:


Create a new "Circuits" project on tinkercad.

Open the drop-down menu on the right-hand side and select "Arduino"

Click on the circuit "Breadboard" and place it somewhere on the workspace.

Build the circuit shown in the above figure by selecting the right components from the menu on the right-hand side. Connect also all components with the required wires. Take good care that you use the correct resistor values. You can use this online tool to find the correct value based on the shown color coding.

Write the required code such that the left most potentiometer controls the red color of the RGB led (potentiometer to the left means red is off, potentiometer to the right means red is completely on), the middle potentiometer controls the blue color, and the right potentiometer controls the green color.

To make the program work you need to make use of the analogRead and analogWrite functions. Note that these two functions have a different range. You can map the range of one function to the other using the map function.

Part 2 - The Photoresistor

A photoresistor is a variable reistsor that changes its resistance based on the brightness level (light intensity) in the environment. You can simulate a changing brightness level in tinkercad by clicking on a photoresistor during simulation and adjusting the sliderbar. The clip below shows how the photoresistor can be used together with the Arduino.


Adapt the circuit you developed in part 1 of this lab by replacing the potentiometers with photoresistors. Note that you also need to add some fixed resistors to create the appropriate voltage dividers. You may again use this online tool to find the correct value for the resistors.

Adapt also the code running on the Arduino to ensure that in full darkness the color RGB led is off. This will require a small adaptation to the input range in the the map functions. Why is this change needed?

Part 3 - The piezoelectic speaker

A piezoelectric speaker is loudspeaker that uses the piezoelectric effect to create sound. This type of loudspeaker is one of the most commonly used loudspeakers nowadays. In this part of the exercise we will extend our circuit to not only control the color of an RGB led, but also to generate a different tone through a piezoelectric speaker. Before adapting the circuit watch this video to better understand how the piezoelectric loudspeaker works and how it can be used with the Arduino.


Adapt the circuit you developed in part 2 of this lab adding a piezoelectric speaker to the circuit Note that you also need to add a fixed resistor in series with the component. You may again use this online tool to find the correct value for this resistor.

Adapt also the code running on the Arduino such that the speaker generates a tone whose frequency depends on the brightness level of the left most photoresistor. In other words, map the analog input range of the photoresistor to the output pitch range 120 - 1500Hz. For this puspose you need to use the function tone.

Part 4 - The servo motor

In this final part of the exercise we will replace the piezoelectric speaker with a servo motor. The rotation of the servo motor will be controlled through the brightness of the left most photoresistor. First watch the below video to see how you should use the servo motor with the Arduino.


Adapt the circuit you developed in part 3 of this lab adding a servo motor to the circuit. Of course you should also remove the piezo electric speaker from the circuit.

Adapt the code running on the Arduino such that the brightness level is sampled once every 100 millisecond (use the delay function). The motor should rotate 10 degrees in the positive direction if the brightness level is increased compared to the previous sample. The motor should rotate 10 degrees in the negative direction if the brightness level is decreased compared to the previous sample. The motor should remain in place in case the brightness level has not changed. Obviously the angle (position) of the servo motor should not exceeds its limits (angle 0 and 180 degrees).

What happens when you slowly increase the brightness level of the photoresistor and then all of a sudden return it back to full darkness?