Intermediate
...


Learning Objectives

  • Emphasis: Equal
  • Hardware: Servo motor
  • Software: Random number generation

Materials

  • Arduino Uno
  • Breadboard and wires
  • Standard Servo Motor
  • 10kohm resistor
  • Pushbutton switch
  • Crayon, colored pencil, or marker
Reference Files

Breadboard Layout

  1. Switch: Connect the switch the same way as always: one end to power, the other through the 10k resistor to ground, and a wire from the switch-resistor joint to pin 7.
  2. Servo Motor: A servo is a small motor that allows you control the angle of rotation. Unlike DC motors or continuous rotation servos, a standard servo only moves from 0-180 degrees. This makes it great for precise applications, like robot arms or sensor control. It's also really easy to hook up. You should have three wires coming from your servo: black, red, and white. Connect the black wire to ground and the red to power. Connect the white to a PWM pin on the Arduino - we used pin 10. That's all there is to it!
  3. Drawing utensil: Our robot can't draw without a pen! Your servo should have come with several plastic parts that hook onto the rotating end of the motor. Attach the long, skinny part and tape on your drawing utensil of choice. Check the video below if you're not sure what this looks like.
  4. You're done! Plug your Arduino into the USB port on your computer.
Reference Schematic
...

...

Software

  1. Getting started: To make a random drawing robot, we need to generate random numbers to set the servo to a random angle at a random interval. This will allow our robot to make random strokes on the paper. Start by declaring ints to store your pin numbers and another int to store the robot state: on (1) or off (0).
  2. Servo setup: To program our servos, we need to import the servo library with the #include statement at the top of the code to the left. Then we declare a servo by creating Servo myServo at the top of our program. Inside our setup method, after we've set our input/output pins, set the servo pin by writing myServo.attach(servoPin). Now you have a valid servo that you can program.
  3. Seed random number generator: We need to initialize our random number using the randomSeed command. This tells our Arduino to generate random numbers based on a nonsense reading from an unconnected pin - we'll use analogRead(0) to tell it to use the noise coming off of pin 0. These values aren't tied to anything, and they're constantly changing, which will allow our numbers to appear as random as possible.
  4. Turn robot on: If we press the button, we want to change the robot state to be the opposite of what it currently is. We do this with a single line of code: robotOn = !robotOn. Then we need to delay to debounce the button.
  5. Generate random numbers: If your robot is turned on, it's time to draw! The random command can either take only an upper-bound (exclusive) or an upper (exclusive) and lower (inclusive) bound. Our servo can take values from 0-180 degrees, but the actual drawing range so it can work on paper is really from about 30-120 degrees. Write a random value between 30-120 degrees to the servo. We'll also delay a random amount so the brush strokes seem more random - generate and delay a random number of milliseconds from 100-500.
  6. Experiment: Try with multiple servos and multiple colors to make a true masterpiece. Can you change the speed or angle of your servo? What if you connect the robot to a light detector so it acts like a motion sensor and only draws when someone is close by? What else can you do with rotating servos?
Reference Code

The Finished Product




<-- Previous: Stopwatch Next: Mood Light -->