2021W1 – Lab 3: Control


Learning Goals

Today will be the first lab where we ask you to work as a team on a shared creative vision. Take a moment to review the rules of brainstorming and collaborative creativity. You will be designing some kind of system that reacts to a distance sensor.


  1. The 7 simple rules of brainstorming. Read this guide to brainstorming and use it while developing your creative idea today. Suggestion for deferring judgement: add a rule where everyone must come up with at least two “bad” ideas but never tell anyone else which ones were the “bad” ones.


Part 1: Ultrasonic sensor

The ultrasonic sensor is a distance sensor. You may have already seen it if you completed the challenge component from Lab 1. If so, you can skip this part.

  1. Build a circuit for the Ultrasonic sensor
    • You can take a look at this resource – scroll down and use the Source code with New Ping.
    • Download and use the New Ping Library!
  2. Program the Arduino to output distance in inches and centimetres.

Part 2: Use the Ultrasonic sensor to control a motor

Review the Motor Controller code from last lab (here), including any modifications you made to it. If you completed the challenge component from the last lab and would like to use that code, skip ahead to step 3 and replace your potentiometer control with Ultrasonic control. Otherwise, integrate the ultrasonic sensor code and the motor control code by:

  1. Copy-pasting the Ultrasonic code into your Motor Controller code. Remember that everything needs to be in the proper place: “setup” code with other setup code, “loop” code with other loop code, etc.
  2. Using the code you just copy and pasted together, confirm that everything works independently, i.e., the motor still drives and the ultrasonic sensor still senses distance. You may have to edit the “delay” amount from the Ultrasonic code and change the pin numbers for either the Ultrasonic Sensor or Motor Driver.
  3. Deleting the former Motor Controller code from the LOOP ONLY (i.e., everything from  boolean isValidInput;  to while( isValidInput == true);  and adding the following to control the motor via distance values (replace pink with your own measured values):
    1. int value = distance
    2. int fromLow = (lowest distance value)
    3. int fromHigh = (highest distance value)
    4. int toLow = 0
    5. int toHigh = 255
    6. int speed = map(value, fromLow, fromHigh, toLow, toHigh)
    7. analogWrite( MOTOR_B_PWM, speed );

You should now be able to control the motor speed with the distance between your hand and the Ultrasonic sensor. Check out the map function online for reference.

Again, you may have to play with delay values. Get it so that you can consistently control the speed of the motor with the distance value. You may want to add a Serial.println statement to monitor the values.

Keep at least one circuit setup intact. Show your TA before moving on.

Part 3: Get Creative

Congratulations! You just made your first gestural human-computer interface. Now it’s time to get creative and play. You must now create something that responds to at least two kinds of inputs. That can mean two ultrasonic sensors OR two types of sensors OR one ultrasonic sensor that differentiates between two kinds of hand motion.

Here are some ideas to get you started:

  1. Keep the hand distance->motor speed control, but restrict the distance range to, say, 30cm and add other functionalities for movements beyond 30cm.
  2. Add a second Ultrasonic sensor and motor and use two hands, say one for controlling speed and another for direction.
  3. Change the distance range of the Ultrasonic sensor with a potentiometer.
  4. ??? Make up your own!

Start by brainstorming ideas with your team and playing with your current sensor setup. Integrating sensors and doing gesture control is very hard. It’s OK if it doesn’t work perfectly. The important part of this is to try something difficult. When you’re done, take a photo or short video and post it on Piazza. Show your TA!


  • If you’re trying to integrate sensors, remember you’ll need global variables for each of the sensor values.
  • You may want to “keep track” of a number of past sensor values. You can define variables such as “lastValue” to keep one previous value, but you can also define an Array of values to track the past N values.
  • Use the Serial monitor to track all of your sensor values.
  • If you’re changing motor directions, put a “stop” statement in between the direction change statements or else the gearbox will be overloaded.

Optional Challenge

If you feel like you’re getting to here, make your gestures more complicated! Add another sensor. Try making an encoder and do position control. Post pix on Discord.