Introduction: Model Police Car

We wanted to build a model police car with working lights and sirens. Then we decided to add a motor and instead of using a switch to turn everything and off we used a photodiode. For our car the photodiode signal is a simple on off switch. If you want a more complicated device that will respond differently to different signals (only do lights, or do lights and drive but no sirens) then you will need a significantly more complicated setup and code. 

Step 1: Gathering the Necessities

Materials, (suggested sources):
-Plastic Model Car, preferably snap together. We used a 1:25 scale and that seemed to be a little on the small side (http://www.amazon.com/gp/product/B000JSNYU6/ref=oh_o00_s00_i00_details)
-3 Superbright red LEDs (http://www.sparkfun.com/products/528)
-3 Superbright blue LEDs (http://www.sparkfun.com/products/529)
-A small buzzer or speaker (http://www.sparkfun.com/products/7950 or http://www.sparkfun.com/products/9151)
-A small motor (http://www.sparkfun.com/products/10171)
-A Darlington transistor (http://www.sparkfun.com/products/312)
-A photodiode (http://uk.rs-online.com/web/p/photodiode/6655331/)
-A flashlight, preferably quite bright. Or possibly a laser pointer, experiment!
-Wire
-2 9V Batteries
-1/4 Watt Resistors: 6 330 ohm resistors for the LEDs, a 100 ohm resistor for the buzzer, a 10,000 ohm resistor for the photo diode.
-Perforated PVC sheet or printed circuit board: You can design and print your own circuit boards if you have the resources. It will make for a tidier interior of the car and an easier soldering process. You can also wire and solder to a perforated sheet of PVC.
-Solder and Soldering Iron
-Breadboard for testing setups
-An Arduino Uno
-The Arduino program
-Gears for your motor and axle?
-Multimeter for testing
-Paint and decorations for your car

If you can get a good gear ratio for your motor the the car will actually be able to drive. We found that the car was too heavy to drive at our current gear ratio, but ran out of time to change it. You may also need to consider getting your car lifted after all the circuitry is inside it. You could also use a larger model and a more powerful motor. Or really just a more powerful motor. 

Step 2: Prepare the Car

With the snap together model cars it is relatively easy to remove all the interior parts and only assemble the base, the wheels and axles and the outside shell. Make sure the wheels and axles are working well and turn freely. Experiment with assembling the car using the minimum number of interior parts, you will need that space to work with. 

For the lights on top of the car, take the outer shell of the car and drill 6 holes in the roof in a line. Use a drill press and a drill bit with a guage slightly less than than the size of the LEDs. Make sure to use mineral oil or another lubricant while drilling because the soft plastic heats up and starts to melt very fast. Clean the drill holes of excess plastic and then widen the holes a tiny bit by hand using the drill bit. Make sure the LEDs fit in tightly.

Take the base of the car and make sure your Arduino can fit comforably. At this point you will need to consider the location for your motor and how you will connect the axle to the motor. Our very kind machinist designed a pulley system for our axle and motor. We cut a section out of the base to allow room for the pulley around the axle and set the motor behind the Arduino between the back wheels. Make sure to leave room for a couple batteries because your ardunio and all the other components need a power source. You may have to lift the body a bit.

Step 3: Wiring the Lights

The lights need to be wired in parallel to shine brightly, but also to have resistors so as not to blow them out. The resistance will vary based on the type of LED you chose and the power source. We used 5mm superbright LEDs with 330 ohm resistors and a 9V battery as a power supply.

Put a resistor in series with each LED, then wire the three red LEDs in parallel with each other and the three blue LEDs in parallel with each other. Wire all the LEDs to a common ground, such as the ground of the battery. Make sure to wire the Arduino's ground to that ground as well.

This is the circuit diagram for our lights, Red Input and Blue Input imply a connection to one of the Arduino's digital ports. There will be a full circuit diagram for how to wire everything to the Arduino later on. 

You could also opt to use 6 RGB LEDs and those can be very bright, so it may be a good option if you have them available. Wire them in the same manner as you would the single color LEDs, using only the pin of the color you want to light up. Or wire separately to both the red and blue pins and play with code for more complicated light sequences.   

Step 4: Wiring the Speaker

Wiring the speaker is one of the simplest parts of this project, but getting it to work was one of the hardest. More on code later, for now we'll just wire up the parts. 

The miniature speaker or buzzer is most likely not polarized, but check yours to make sure. It should also only require about 100 ohms of resistance, but again this will vary with your setup and specific parts. 

Simply wire the resistor in series between the input and the speaker and the other side of the speaker to ground. See circuit diagram below. 

We used a 9V battery and a 100 ohm resistor with our speaker. 

Step 5: Wiring the Motor

This is the most complicated wiring section, as it consists of two separate circuits. The first is the photo diode circuit which requires it's own power source and provides the signal to the car for whether it should turn on or off. The second is the motor circuit.

The best description of this wiring is shown in the diagram below, but a short explanation will also be useful. When the photo diode is not sensing light there is no current flowing through it and thus no current flowing through the resistor so the reading at A0 should be high (~9V). When light activates the photo diode it starts drawing current and there will be a voltage drop across the resistor, causing the reading at A0 to go low (~0V). This change in reading will be interpreted through the Arduino and sent as a signal to the motor input. The Darlington transistor helps amplify the current so the motor can run. 

Toy motors aren't usually polarized but the darlington transistor definitely is. Make sure you have it connected the right way, so the collector is attached to D7 and the emitter is attached to the far side of the motor. 

Step 6: Putting It All Together

Now we just need to wire all the different parts to the Arduino, and make sure to connect all the grounds together so they all have the same value. The diagrams below show the Arduino and which ports should be connecting to which circuits, and the full diagram with all the parts together. Make sure to use enough wire so that you can maneuver the parts around the inside of the car. When you have everything wired to the Arduino zip-tie all the wires attached to the Arduino together and to the hole in the Arduino board. Then they will be secure. 

Step 7: Putting Everything in the Car

Experiment with different configurations, being sure to leave room for your mechanism connecting the motor and the axle. We used a pulley type system. 

Step 8: The Code

This was our code. If you used different ports or different parts then you may need to change the code slightly.  This code will run one cycle of lights and sirens while running the motor every time the photodiode senses the LED light.

CODE

/*
  Motor, lights, and siren code with LED.

  Drives a motor, lights and siren with input of LED light

  circuit:
  *one small motor
  *small speaker
  *six LED's
  *one photodiode

  Created 4-17-12
  by Dalton Bolger and Dulcie Head

*/

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations = 16;

int IR = A0;
int motor = 7;
int blueLed = A5;
int redLed = A4;

// creates the state for the lights and intializes the time
int ledState = LOW;
long previousMillis = 0;

// time in between flashes for the lights
long interval = 100;

void setup() {
  pinMode(IR, INPUT);
  pinMode(motor, OUTPUT);
  pinMode(blueLed, OUTPUT);
  pinMode(redLed, OUTPUT);
  Serial.begin(9600);
}

// method to create the lights without having to use a delay
void runLights() {
   unsigned long currentMillis = millis();
  if (currentMillis - previousMillis > interval){
    previousMillis = currentMillis;
    digitalWrite(A4, ledState);
    if (ledState == LOW)
      ledState = HIGH;
     else
       ledState = LOW;
     digitalWrite(A5, ledState);
  }
}

// run the sirens while flashing lights continuously
void lightsAndSirens() {
  int melody = 550;
  while (melody<1200) {
    runLights();
    // to calculate the note duration, take one second
    // divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDuration = 1000/noteDurations;
    tone(8, melody, noteDuration);
    melody = melody + 7;

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = noteDuration * .5;
    delay(pauseBetweenNotes);
    // stop the tone playing:
    noTone(8);
  }
  melody = 1200;
  while (melody > 550) {
    runLights();
    int noteDuration = 1000/noteDurations;
    tone(8, melody, noteDuration);
    melody = melody - 7;

    int pauseBetweenNotes = noteDuration * .5;
    delay(pauseBetweenNotes);
    noTone(8);
  }
}


// if the appropriate light shows up, run the motor, sirens, and lights
void loop() {
  int speeds = analogRead(IR)*1.5;
  Serial.println(speeds);
  if (speeds > 40 && speeds < 500) {
    digitalWrite(motor, speeds);
    lightsAndSirens();
  }else {
    analogWrite(redLed, LOW);
    analogWrite(blueLed, LOW);
    analogWrite(motor, LOW);
  }
  delay(5);
}

Our test run of the code: http://www.youtube.com/watch?v=-BTyNu-XDLA&feature=youtu.be

Step 9: Final Product

After all is said and done, we were able to fit everything in the car (with a bit of a lift kit ;) ), but unfortunately, the car was a bit too heavy to drive itself on flat ground.  Below we have a working video of the car running lights and sirens, but no motor while the weight of the car is on it.  We also have a video of the lights, sirens, and motor all working when there is no weight on the back tires.  Enjoy and good luck!

Dulcie Head and Dalton Bolger
Pomona College

Test Drive on Table: http://www.youtube.com/watch?v=FxSsUAD_-vc&feature=youtu.be
Test Back Wheels: http://www.youtube.com/watch?v=hbLg-QDK5a0&feature=youtu.be

Driving down an incline! http://www.youtube.com/watch?v=Kx1m3e634-0&feature=g-upl

Make It Real Challenge

Participated in the
Make It Real Challenge