Arduino Servo Motor Tutorial

A servomotor is a rotary actuator or linear actuator that allows for precise control of angular or linear position, velocity and acceleration.
Servomotors are used in applications such as robotics, CNC machinery or automated manufacturing.
This tutorial will show you how to use the principals of a servo motor with your Arduino board.
Background:
This code moves the shaft of a RC servo motor back and forth across 180 degrees.
This example makes use of the Arduino servo library.

Components:
1 Arduino Board
1 Servo Motor
Hookup wires

Directions:
1.Servo motors have three wires: power, ground, and signal. The power wire is typically red, and should be connected to the 5V pin of your Arduino board. The ground wire is typically black or brown and should be connected to a ground pin on the Arduino board. The signal pin is typically yellow, orange or white and should be connected to pin 7 on the board.
2. Input following code into your Arduino Sketch and compile.
3. Plug USB A/B Cable into your laptop and connect it to your Arduino.
4. Press the upload program to run the code. Notice how the Servo motor will move 0 to 180 degrees clockwise, pause, then move 180 degrees in the reverse direction.

Code:

// This code will move the servo back and forth from 0-180 degrees.
#include

Servo servoobj; // create servo object to control a servo

int var = 0; // variable to store the servo position

void setup() {
servoobj.attach(7); // attaches the servo on pin to the servo object
}

void loop() {
for (var = 0; var <= 180; var += 1) { // goes from 0 degrees to 180 degrees // in steps of 1 degree servoobj.write(var); // tell servo to go to position in variable ‘var’ delay(15); // waits 15ms for the servo to reach the position } for (var = 180; var >= 0; var -= 1) { // goes from 180 degrees to 0 degrees
servoobj.write(var); // tell servo to go to position in variable ‘var’
delay(15); // waits 15ms for the servo to reach the position
}
}

Arduino Tutorial: Dog Door Buzzer

This Arduino tutorial will use a photoresistor and a piezo buzzer to create a buzzer for your dog door to know when your best friend comes and goes. A photoresistor is a light-controlled variable resistor whose resistance  decreases with exposure to light. As your dog passes through the door, the photoresistor’s resistance will decrease and cause thepiezo buzzer to sound.

 

Components:

1 Arduino Board

1 Breadboard

1 Photoresistor

1 Piezo Buzzer

1 10K ohm resistor

1  6” USB A/B cable

Hookup Wires

 

Directions:

  1. Start off by placing your photoresistor on the breadboard. Connect a wire from the 5V source to the photoresistor. Place a 10k Ω resistor on the other pin of the photoresistor. Connect a wire from the resistor to the A0 pin and a wire from the other leg of the resistor to the ground of your Arudino board.
  2. Connect a wire from digital pin 7 to the piezo buzzer on the breadboard. Be sure to ground the Piezo Buzzer.
  1. Open the Arduino software and create a new sketch; then upload the code below into the sketch:

int rcvpin=A0;

int buzzpin=7;

 

void setup()

 

{

 

pinMode(rcvpin,INPUT);

 

pinMode(buzzpin,OUTPUT);

 

buzz(200);

 

buzz(200);

 

buzz(200);

 

delay(2000);

 

Serial.begin(9600);

 

}

 

void loop()

 

{

 

int ldrval=analogRead(rcvpin);

 

Serial.println(ldrval);

 

if(ldrval>=500)

 

{

 

buzz(50);

 

}

 

}

 

void buzz(unsigned char time)

 

{

 

analogWrite(buzzpin,170);

 

delay(time);

 

analogWrite(buzzpin,0);

 

delay(time);

 

}

 

4.Connect a USB A/B cable from your computer to the Arduino. Click “Upload” in the Arduino program to run your sketch.

 

5.To test your code, cover the photoresistor to buzz the piezo buzzer.

Learning with Arduino: How to use a Thermistor

A thermistor is a type of resistor whose resistance is dependent on temperature.
Thermistors are of two opposite fundamental types:
With Negative Temperature Coefficient, NTC, resistance decreases with temperature to protect against inrush overvoltage conditions.
With Positive Temperature Coefficient, PTC, resistance increases with temperature to protect against overcurrent conditions.
The purpose of this tutorial is to understand how to use a thermistor to create a reminder to turn on either your AC or your heat based on the temperature in your house.
Note: The temperature setting in this tutorial corresponds to a specific voltage that comes out of the Analog to Digital Convertor (ADC) Reading:
Analog to Digital Converter (ADC) reading = (Voltage at Pin / 5V) * 1023
Extra Tip: To select a ADC reading based on the temperature setting of your choice, use the Steinhart-Hart equation to convert the ADC voltage readings to a setpoint temperature.

Components:
Arduino Board
Breadboard
Thermistor
1 6” USB A/B cable
3 220 ohm resistor
2 LEDs
Hookup Wires

Directions:
1. Place your thermistor on the breadboard. Connect a wire from the 5V source to the thermistor. Place a 220 Ω resistor on the other pin of the thermistor. Connect a wire from the resistor to the A0 pin. Connect a wire from the other leg of the resistor to the ground of your arudino board.
2. Connect a wire from digital pin 9 to your breadboard and then place a 220ohm resistor and a blue LED on the breadboard. Be sure to ground the LED.
3. Connect a wire from digital pin 8 to your breadboard. Next,, place a 220ohm resistor and a red LED on the breadboard. Be sure to ground the LED.

 

THE CODE:
4. Open the Arduino software and create a new sketch. Upload the code (seen below) into the sketch.

// LEDS will indicate when the heat needs to be turned on and when the AC needs to be turned on.

//400=60 degrees farenheit
//525=80 degrees farenheit

int tempPin = 0;
int temp; // the analog reading from the sensor divider
int ACSensor = 9; // connect Blue LED to pin 9
int HEATSensor = 8; // connect Red LED to pin 8
int HEATon= 400;
int ACon= 525;
void setup(void) {
Serial.begin(9600);
}

void loop(void) {
temp = analogRead(tempPin);

Serial.print(“Temp = “);
Serial.println(temp); // reading the values

if (temp <= HEATon){ digitalWrite (ACSensor, LOW); digitalWrite (HEATSensor, HIGH); } else if (temp >= ACon){
digitalWrite (ACSensor, HIGH);
digitalWrite (HEATSensor, LOW);
}
else
{
digitalWrite (ACSensor, LOW);
digitalWrite (HEATSensor, LOW);
delay(10);
}
}

5. Connect a USB A/B cable from your computer to the Arduino. Click “Upload” in the Arduino program to run your sketch.
6. Finally, to test your code,  set both the AC and heat temperature setting to the current temperature setting in your house to light the LEDs.