The Next Best Circuit - Circuit #10 (Otherwise Known as The Spinning Motor*)

*and Surprise!

For week #5, we could do what we wanted and I thought I would work with a spinning motor. It sounded interesting and I thought it would be a challenge. The toy motor reminded me of one used in remote control cars. The build consisted of a transistor, a diode, a DC motor, a resistor, and six jump wires. 

I built the wiring and connected everything first. It looked great and I was excited. Then, I added a fan! So I thought about it and it would be hard to see the motor running unless something was hooked up to it. 

Then I put the code in and ran the sketch, but it didn’t work. It came up with an error message. I was ready this time though. I found another site and compared the code and used it instead. The second code worked great! I was ready to try it out! Then, it didn’t work. So, I changed the motors and my daughter noticed that the new motor worked. We attached the fan so everyone could see it run. By the time I was done, even my roommate was checking it out. It was AWESOME!

Follow this link to the explanation and this one to results

The Code

 
 

/********************************************************************
* SparkFun Inventor's Kit
* Example sketch 12
*
* SPINNING A MOTOR
*
* This example requires that you drive your motor using a switching
* transistor. The Arduino is only capable of sourcing about 40 mA of
* current per pin and a motor requires upwards of 150 mA.
*
* Look at the wiring diagram in the SIK Guide - Circuit #12 or read the
* notes in the readme tab for more information on wiring.
*
* This sketch was written by SparkFun Electronics,
* with lots of help from the Arduino community.
* This code is completely free for any use.
* Visit http://learn.sparkfun.com/products/2 for SIK information.
* Visit http://www.arduino.cc to learn about the Arduino.
*
* Version 2.0 6/2012 MDG
* Version 2.1 8/2014 BCH
*******************************************************************/

const int motorPin = 9; // Connect the base of the transistor to pin 9.
// Even though it's not directly connected to the motor,
// we'll call it the 'motorPin'

void setup()
{
pinMode(motorPin, OUTPUT); // set up the pin as an OUTPUT
Serial.begin(9600); // initialize Serial communications
}


void loop()
{ // This example basically replicates a blink, but with the motorPin instead.
int onTime = 3000; // milliseconds to turn the motor on
int offTime = 3000; // milliseconds to turn the motor off

analogWrite(motorPin, 255); // turn the motor on (full speed)
delay(onTime); // delay for onTime milliseconds
analogWrite(motorPin, 0); // turn the motor off
delay(offTime); // delay for offTime milliseconds

// Uncomment the functions below by taking out the //. Look below for the
// code examples or documentation.

// speedUpandDown();
// serialSpeed();
}

// This function accelerates the motor to full speed,
// then decelerates back down to a stop.
void speedUpandDown()
{
int speed;
int delayTime = 20; // milliseconds between each speed step

// accelerate the motor
for(speed = 0; speed <= 255; speed++)
{
analogWrite(motorPin,speed); // set the new speed
delay(delayTime); // delay between speed steps
}
// decelerate the motor
for(speed = 255; speed >= 0; speed--)
{
analogWrite(motorPin,speed); // set the new speed
delay(delayTime); // delay between speed steps
}
}


// Input a speed from 0-255 over the Serial port
void serialSpeed()
{
int speed;

Serial.println("Type a speed (0-255) into the box above,");
Serial.println("then click [send] or press [return]");
Serial.println(); // Print a blank line

// In order to type out the above message only once,
// we'll run the rest of this function in an infinite loop:

while(true) // "true" is always true, so this will loop forever.
{
// Check to see if incoming data is available:
while (Serial.available() > 0)
{
speed = Serial.parseInt(); // parseInt() reads in the first integer value from the Serial Monitor.
speed = constrain(speed, 0, 255); // constrains the speed between 0 and 255
// because analogWrite() only works in this range.

Serial.print("Setting speed to "); // feedback and prints out the speed that you entered.
Serial.println(speed);

analogWrite(motorPin, speed); // sets the speed of the motor.
}
}
}

 
 
 
 

I was excited this week because I could choose what I wanted to do and I wanted to go big. When I thought it wasn’t going to work out it was disappointing, but I learned from my last mistake and tried to be ready. In this case it worked out. Since we were using a breadboard and not soldering our connections sometimes they are not as solid as they should be. That was the case in my build. I had a loose wire. When I checked them to make sure they were connected properly, it worked. You can see from my excitement and when I attached the fan the motor was turning. Just like it would for a toy car. This was a great build! 

Oh, the surprise was the fan. I hope you enjoyed it.  

 

Thanks!

Create Your Own Website With Webador