Class 4 – Labs with Servo Motor and Tone Output, and Quiz


Lab: Servo Motor Control with an Arduino

giphy_servo with force resistor

Do the wave.

Here I am controlling a servomotor’s movement & position by sending it the value returned from a force resistor.

Question: Why is the syntax to create an instance of the servomotor different than a typical variable or instance? For example:

Servo servoMotor; <— creating an instance of the servomotor using the Servo library.


int servoPin = 3; <— creating an instance of the servo connection to pin 3.


Lab: Tone Output Using An Arduino

Play Tones

I may have shortcircuited a speaker, so instead I’m using a piezo (aka what sounds like my new pet mosquito). I sent a frequency to the piezo that changes depending on how much light the photo resistors are receiving.

Question: What does the 100 – 880 range for the piezo/speaker really refer to? Two frequencies being mixed together to create one? See below.

void loop() {
int sensorReading = analogRead(A0);
float frequency = map(sensorReading, 200, 900, 100, 880);
tone(8, frequency, 10);


A more complex example

This example plays a short melody. This is a great example of how a program runs through code one instruction at a time, in a loop, until its done!


A Musical Instrument

Below you’ll hear me playing Hot Cross Buns. In this exercise I learned that you can create three instances at one time while setting up a for loop. For example: for (int thisSensor = 0; thisSensor < 3; thisSensor++).

Question: However, I’m not certain I understand this way of creating three “thisSensors” is the most efficient or clear? 

Questions: What does the syntax [ ] mean and do? I believe it relates to creating a list… This will probably come up in ICM.




Question about the quiz. I believe the first part of the answer below is 0 – 1024 points. Is the second answer that 10 bits required to use the analogRead() command equal 80 bytes of memory (10 bits x 8 bytes in each bit = 80 bytes? )

  1. Microcontrollers read a variable input using an analog-to-digital circuit inside the controller. The Arduino Uno can read the input voltage with a resolution of 10 bits using the analogRead() command. Give the numeric range (decimal range) for this resolution. How much memory does this take up?

Another question: I found this part in the online syllabus. There is one connected idea in the paragraph below doesn’t make sense to me yet. Why is 10 bits a range of 1024 points?

The ADC in the Arduino can read the input voltage at a resolution of 10 bits. That’s a range of 1024 points. If the input voltage range is 0 to 5 volts, that means that the smallest change it can read is 5/1024, or 0.048 Volts. When you take a reading with the ADC using the analogRead() command, the microcontroller stores the result in memory. It takes an int type variable to store this, because a byte is not big enough. A byte can hold only 8 bits, or a range from 0 to 255. 

Finally, just a note to myself that that you need to use an int type of variable / data to store the readings you might get from analogRead(), so that you can access enough of the computer’s memory to store that many numbers. By using an integer or int data type (or variable), you use up 16 bits of memory, which is enough to house the 10 bits used to read input voltage at a resolution.