Project 1: Motor with LED Speed level

This post follows the stages taken in making a Potentiometer controlled motor with LED speed level. The project came about after playing with a few example codes and using different sensors and outputs and making use of what I found interesting and enjoyable.

The main inspiration for making this project was the LED bar graph example on the arduino website. I had a go at doing it myself and found it really fun to use and thought it would be a good starting point for a project. The bar graph uses 10 LEDs and a potentiometer to turn them on in a row as it is twisted in one direction and turns them off if twisted the other.

LED bar graph Code

/*
LED bar graph

Turns on a series of LEDs based on the value of an analog sensor.
This is a simple way to make a bar graph display. Though this graph
uses 10 LEDs, you can use any number by changing the LED count
and the pins in the array.

This method can be used to control any series of digital outputs that
depends on an analog input.

The circuit:
* LEDs from pins 2 through 11 to ground

created 4 Sep 2010
by Tom Igoe

This example code is in the public domain.

http://www.arduino.cc/en/Tutorial/BarGraph
*/

// these constants won’t change:
const int analogPin = A0;   // the pin that the potentiometer is attached to
const int ledCount = 10;    // the number of LEDs in the bar graph

int ledPins[] = {
2, 3, 4, 5, 6, 7,8,9,10,11 };   // an array of pin numbers to which LEDs are attached

void setup() {
// loop over the pin array and set them all to output:
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
pinMode(ledPins[thisLed], OUTPUT);
}
}

void loop() {
// read the potentiometer:
int sensorReading = analogRead(analogPin);
// map the result to a range from 0 to the number of LEDs:
int ledLevel = map(sensorReading, 0, 1023, 0, ledCount);

// loop over the LED array:
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
// if the array element’s index is less than ledLevel,
// turn the pin for this element on:
if (thisLed < ledLevel) {
digitalWrite(ledPins[thisLed], HIGH);
}
// turn off all pins higher than the ledLevel:
else {
digitalWrite(ledPins[thisLed], LOW);
}
}
}

 Circuit board layout

BarGraph_bb

This is the layout used for this example using the parts:

– Potentiometer

– Jumper wires

– 10 LEDs

– 10 10ohm resistors

Video of the working graph

Next step taken

My thoughts for developing on from the bar graph were based on a standard desk fan where you only have 3 button speed options. I wanted to use to use the ptoentiometer to give move control over the motor speed and as a result the fan. The LEDs would then give a rough ten level indicator for the current speed the motor would be at.

I first worked on the code and setup for running the motor off of the potentiometer:

Motor with Potentiometer code

int sensorPin = 0; // define the pin the sensor is connected to
int motorPin = 9; // define the pin the motor is connected to
int sensorValue = 0; //variable to store the value coming in from the sensor
void setup()
{
  Serial.begin(9600);   // initialize serial communication at 9600 bits per second:
  pinMode(motorPin, OUTPUT); //set motorPin to OUTPUT to provide current to the motor
}
void loop()
{
  motorwithSpeed(); //set the name of the loop
}
void motorwithSpeed(){ //call the loop to run
  sensorValue = analogRead(sensorPin);
  analogWrite(motorPin, sensorValue/4); //set the range to 0-225
  Serial.println(sensorValue/4); // read the value of sensorPin
  delay(1);        // delay in between reads for stability
}

Motor-Breadboard-Layout-with-Pot-300x266

This uses:

– Motor

– Diode

– Jumper wires

– Potentiometer

– Resistor

– NPN Transistor

 

After I had got both the LED bar graph and motor working separately with the potentiometer I combined the code and added the two circuit layouts together.

Combined Code

int sensorPin = 0; // define the pin the sensor is connected to

int motorPin = 13; // define the pin the motor is connected to

int sensorValue = 0; //variable to store the value coming in from the sensor

const int analogPin = A0; // the pin that the potentiometer is attached to
const int ledCount = 10; // the number of LEDs in the bar graph

int ledPins[] = {
2, 3, 4, 5, 6, 7,8,9,10,11 };

void setup()
{
{

Serial.begin(9600); // initialize serial communication at 9600 bits per second:

pinMode(motorPin, OUTPUT); //set motorPin to OUTPUT to provide current to the motor

}
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
pinMode(ledPins[thisLed], OUTPUT);
}
}
void loop()
{

motorwithSpeed(); //set the name of the loop

int sensorReading = analogRead(analogPin);
// map the result to a range from 0 to the number of LEDs:
int ledLevel = map(sensorReading, 0, 1023, 0, ledCount);

// loop over the LED array:
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
// if the array element’s index is less than ledLevel,
// turn the pin for this element on:
if (thisLed < ledLevel) {
digitalWrite(ledPins[thisLed], HIGH);
}
// turn off all pins higher than the ledLevel:
else {
digitalWrite(ledPins[thisLed], LOW);
}
}
}
void motorwithSpeed(){ //call the loop to run

sensorValue = analogRead(sensorPin);

analogWrite(motorPin, sensorValue/4); //set the range to 0-225

Serial.println(sensorValue/4); // read the value of sensorPin

delay(1); // delay in between reads for stability

}

Components and layout

– Motor

– Potentiometer

– 11 10ohm resistors

– Diode

– 10 LEDs

– NPN Transistor

– Jumper wires

project 1 diagram_bb

 

Final product

Photo 20-04-2015 21 20 12

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s