Appearance

Dark Light

Did You Know?

Conventional farmers use around 300 different pesticides to grow foods that are sold in supermarkets everyday.

Using Motors With the Arduino

If you want to use the Arduino to make things move, you'd probably need a motor. You can run (5V DC) motors directly off the Arduino board/pins, but there's a motor shield that you can use to make things easier (and to power motors that need an external power supply).

Parts:


I have a small HiTEC servo motor that can be driven by the motor shield or directly with the Arduino. There are 3 pins on the servo: power, ground and control (red/black/yellow wires). It can go +/- 180 degrees. The code below moves it +180.

Code


#include <Servo.h>

Servo myservo;  

void setup() {
  //attach servo to pin 10
  myservo.attach(10);  
}

void loop() {
  for (int x=0; x<=180; x++) {
      myservo.write(x);

      // waits for the servo to get there
      delay(15);                         
  }
   
}


When using inputs from sensors to control motors, you can do some interesting/useful things; I have it connected to a photoresistor (light sensor). You could hook up the servo to your window blinds and program the Arduino to control the brightness in the room by automatically opening/closing the blinds using the input from the photoresistor.


#include <Servo.h>

Servo myservo; 

//analog pin of photoresistor input
int inputPin = 0;  

int mappedVal = 0;

// value from the analog pin
int val;    

void setup() {
  // attach the servo to pin 10
  myservo.attach(10);  
  Serial.begin(9600);
}

void loop() {
  val = analogRead(inputPin); 

  // scale it to use it with the servo 
  mappedVal = map(val, 0, 100, 0, 180); 

  myservo.write(mappedVal);                       
  Serial.println(mappedVal);
  delay(500);                              
}