Build a spin-art drawing machine

A DC motor spins a paper disc while a servo swings a pen across it: every press of the button draws a new spiral flower pattern, and the knob sets the speed.
  • Level:Intermediate
  • Duration: 1 h 50 min
  • Ages 12+
  • Platform: Arduino

Tools for this lesson

Helpful for this build. Open a tool to see tips and more projects that use it.

Build a spin-art drawing machine – EEMENTOR STEM™ lesson

Machines that draw are called plotters, and the simplest ones make beautiful patterns by combining two movements. In this capstone, the DC motor spins a paper disc like a record player, and a servo swings a pen in and out across it. Spinning and swinging together draw spirals, loops and flower shapes. Every press of the button picks a new swing pattern at random, and the knob sets how fast the disc turns, so no two drawings are the same.

What you'll learn

  • How two simple movements combine into complex patterns
  • How to drive a DC motor with the L293D and PWM
  • How to move a servo along a sine wave
  • Which UNO pins still support PWM when the Servo library is in use

What you need

  • UNO R3 board and USB cable (UNO R3 Starter Kit)
  • L293D motor driver chip and 3–6 V DC motor (UNO R3 Starter Kit)
  • Servo motor (UNO R3 Starter Kit)
  • 10 kΩ potentiometer and 1 push button (UNO R3 Starter Kit)
  • A felt-tip pen, a paper disc about 10 cm across, cardboard, tape and modelling clay
  • Breadboard and jumper wires (UNO R3 Starter Kit)

Make it real: glue the paper disc to a cardboard disc and push a bottle cap on the motor shaft as the hub. Stand the motor upright in modelling clay. Tape the pen to a 10 cm card strip on the servo horn, so that the pen tip rests lightly on the disc about halfway out.

Wiring

The L293D sits across the gap in the middle of the breadboard, notch on the left. Pin 1 is bottom left; the pins are counted anticlockwise.

Breadboard wiring diagram: Build a spin-art drawing machine
Breadboard view. Check each connection against the wiring table below.
From To
L293D pin 1 (EN1) UNO pin 5
L293D pin 2 (IN1) UNO pin 7
L293D pin 7 (IN2) UNO pin 8
L293D pins 3 and 6 (OUT1, OUT2) The two motor wires
L293D pins 4, 5, 12, 13 GND
L293D pins 8 and 16 (VCC2, VCC1) 5V
Servo orange / yellow wire UNO pin 3
Servo red wire 5V
Servo brown / black wire GND
Potentiometer outer pins 5V and GND
Potentiometer middle pin UNO pin A0
Push button, one leg UNO pin 2
Push button, the diagonally opposite leg GND

The sketch

#include <Servo.h>

const int EN_PIN = 5;        // motor speed: a PWM pin that the Servo library leaves free
const int IN1_PIN = 7;
const int IN2_PIN = 8;
const int PEN_PIN = 3;
const int SPEED_PIN = A0;
const int BUTTON_PIN = 2;

const int PEN_MIDDLE = 90;   // pen halfway out on the disc
const int PEN_SWING = 25;    // how far the pen swings in and out (degrees)

Servo pen;
bool drawing = false;
int lastButton = HIGH;
float phase = 0;
float swingSpeed = 1.0;      // how fast the pen swings compared with the disc
float wobble = 0.0;          // a second, slower swing on top

void startDrawing() {
  swingSpeed = random(10, 60) / 10.0;     // 1.0 ... 5.9: a new pattern every time
  wobble = random(0, 30) / 100.0;
  phase = 0;
  drawing = true;
  Serial.print("New pattern: swing ");
  Serial.print(swingSpeed);
  Serial.print(", wobble ");
  Serial.println(wobble);
}

void stopDrawing() {
  drawing = false;
  analogWrite(EN_PIN, 0);
  pen.write(PEN_MIDDLE);
}

void setup() {
  Serial.begin(9600);
  pinMode(EN_PIN, OUTPUT);
  pinMode(IN1_PIN, OUTPUT);
  pinMode(IN2_PIN, OUTPUT);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  digitalWrite(IN1_PIN, HIGH);            // always the same direction
  digitalWrite(IN2_PIN, LOW);
  pen.attach(PEN_PIN);
  pen.write(PEN_MIDDLE);
  randomSeed(analogRead(A5));
}

void loop() {
  int button = digitalRead(BUTTON_PIN);
  if (button == LOW && lastButton == HIGH) {     // button: start or stop
    if (drawing) stopDrawing();
    else startDrawing();
    delay(200);
  }
  lastButton = button;

  if (!drawing) return;

  int speed = map(analogRead(SPEED_PIN), 0, 1023, 100, 255);   // below ~100 the motor stalls
  analogWrite(EN_PIN, speed);

  phase += 0.05 * swingSpeed;
  float swing = sin(phase) + wobble * sin(phase * 0.13);       // main swing + slow wobble
  pen.write(PEN_MIDDLE + (int)(PEN_SWING * swing / (1.0 + wobble)));
  delay(20);
}

Build it in stages

  1. Motor. Wire the L293D and the motor, and the potentiometer. Upload the sketch and press the button: the motor turns, and the knob changes its speed.
  2. Pen arm. Add the servo without the pen. Press the button: the arm swings in a different rhythm each time.
  3. Draw. Fix the pen so its tip rests lightly on the paper, start the motor, and press the button to begin. Press again to stop; change the paper and press for a new pattern.

How it works

The disc turns at a steady speed while the pen swings in and out along a sine wave. If the pen made exactly one swing per turn of the disc, you would see a simple off-centre circle. Because swingSpeed is a random number like 3.7, the swings and the turns never quite line up, and the pen draws loops that slowly rotate round the disc into a flower pattern. The wobble adds a much slower second swing on top, which makes the petals grow and shrink.

The Servo library uses one of the UNO's timers, which switches off PWM on pins 9 and 10. That is why the motor's speed pin EN1 is on pin 5, which keeps its PWM, and the servo is on pin 3. The motor always turns the same way here, so IN1 and IN2 are set once in setup().

Try this next

  • Use a second pen colour and draw two patterns on top of each other.
  • Show the pattern numbers on the LCD so you can draw a favourite again.
  • Let the pen swing speed slowly change during the drawing for spiralling patterns.

Troubleshooting

  • The motor doesn't turn: check the L293D notch is on the left, pins 8 and 16 on 5V and pins 4, 5, 12 and 13 on GND. Turn the knob up.
  • The pen skips or tears the paper: the pen presses too hard. Let it rest lightly on the paper, or make the arm a little longer.
  • The UNO restarts: the motor and servo together need a lot of current. Use a powered USB hub or a 9 V power supply on the UNO's barrel jack.

Build it yourself

Everything used in this lesson comes in one kit, with parts tested by our engineers.

UNO R3 Starter Kit | Arduino IDE Compatible | Free Tutorials $56.00

Shop the kit