arduino

How to convert a DC motor to a servo motor with Arduino

DC motor is very simple to operate, you power it and it rotates in one direction and if you change the polarity of the power it rotates in the opposite directions

Advantages of DC motor

  • Low cost
  • Easy to get going, all you need is a power supply
  • High RPM ( High speed )

Disadvantages

  • Low Torque: Not practical to use it the drive a toy without gears
  • Position control is not possible
  • Low speeds are difficult to achieve

Servos, have few advantages over DC motors:

  • High Torque
  • Low speed
  • Precision control

One problem with servos is that they cost a lot more than DC motors.

How to convert a DC motor to a Servo motor:

The basic idea behind converting a DC motor to servo is to find the position of the shaft and apply a DC voltage to get the Shaft to the expected position.

The below diagram illustrates the idea:

Controlling a DC motor in Servo mode:

Items required

Ref: http://www.instructables.com/id/DIY-Servo-Motor/


//Arduino code to control angle of a motor shaft using a potentiometer for feedback

//Please use a low rpm motor. Not more than about 500 rpm.

//******POTENTIOMETER SETUP *************

//1. Fix the shaft of the potentiometer to the motor shaft.You might like to use a slightly flexible coupling

//to do this, otherwise even a slight misalignment may cause trouble.

//2. Fix the body of the potentiometer to a rigid surface such as the body of the motor,//so that when the motor shaft turns, only the potentiometer shaft turns with it.

//3. Now we can read the potentiometer value to get the angle of the motor shaft//Look at my youtube video to see how I did this. In my video, I fixed the BODY of the potentiometer//to the motor shaft. It will be better to fix the SHAFT of the potentiomter to the motor

//shaft if you can do it properly

//Fix santa's hand to the motor shaft so that it does not interfere with the potentiometer movement

intpotPin=5;

//we will read the potentiometer value on analog pin 5

//**********************************************

//*****ANGULAR CONSTANTS********

#definePOT_VALUE_MAX700

// potentiometer reading when motor shaft is at 180 degree position.

//You will need to fill this value according to your setup.See below....

#definePOT_VALUE_MIN200

//potentiometer reading when when motor shaft is at 0 degree position.

//You will need to fill this value according to your setup.See below....

//To fill up the correct values, first turn the motor shaft manually to 0 degree position.

//Now read the potentiometer value and edit the #define POT_VALUE_MIN line with your pot reading.

//Next manually move the motor shaft to 180 degree position,

//read the pot value and edit #define POT_VALUE_MAX line with your pot reading.

#definePERM_ERROR3//the max permissible error in degrees. In my potentiometer, a turn only about 3 degrees

//on the potentiometer shaft causes any real change in the ohmic reading. You can adjust this error

//value as required. If PERM_ERROR is very small, the shaft will keep hunting left and right as the//analogRead() of the potentiometer pin keeps fluctuating slightly

#defineMAX_ANGLE180//we will allow our motor to turn by a maximum angle of 180 degrees

//**********************************

//Now we define a class that will control our DC motor....

//This motor must be running from a H bridge like L298 IC

//********************************************************************

classDCMotor{

private:
   
int M_pin1,M_pin2,M_PWMPin; 
int M_Speed;  
int turnDirection;      
enum turnDirection{right,left};

public:

DCMotor(int p1,int p2,int p3)//Constructor
   
{
           
    M_pin1=p1;//direction pin on L298
           
    M_pin2=p2;
 
    //direction pin on L298
           
    M_PWMPin=p3;
 
    //PWM pin on L298
                       
    pinMode(M_pin1,OUTPUT);
                       
    pinMode(M_pin2,OUTPUT);
   
}
   
void SetTurnDirection(intdir)//Setting turn directions on L298 IC
   
{
       
    turnDirection=dir;
       
    switch(turnDirection)
       
    {
           
        case right://turning Right
                   
            //motor moves CW
                   
            digitalWrite(M_pin1,HIGH);
                   
            digitalWrite(M_pin2,LOW);
                   
        break;
           
        case left://turning Left
                   
            //motor moves CCW
                   
            digitalWrite(M_pin1,LOW);
                   
            digitalWrite(M_pin2,HIGH);
               
            break;
       
    }
   
}
   
void SetTurnSpeed(ints)
   
{
        
    M_Speed=s;
   
}
       
void Turn()
   
{
       
    analogWrite(M_PWMPin,M_Speed);
   
}
       
voidStop()
   
{
       
analogWrite(M_PWMPin,0);
   
}
       
voidGoToAngle(inttarget,inthowFast)
   
{
     
    //find out the current angle of the motor shaft
       
    int currentAngle=((float)analogRead(potPin)-POT_VALUE_MIN)/(POT_VALUE_MAX-POT_VALUE_MIN)*MAX_ANGLE;
           
    //First Check if we need to turn left or right.....
     
    if(currentAngle<target)
     
    {
       
        SetTurnDirection(right);
     
    }
     
    else if(currentAngle>target)
     
    {
       
        SetTurnDirection(left);
     
    }
           
    SetTurnSpeed(howFast);
           
    while(abs(currentAngle-target)>PERM_ERROR)//if the shaft is not at the required value,
     
    {
       
        Turn();//Keep on turning the shaft
               
        //Allow the motor to turn a little and wait here for a moment...
               
        delay(100);//adjust the delay as required depending on your motor speed
               
        //update the current angle of the shaft
               
        currentAngle=((float)analogRead(potPin)-POT_VALUE_MIN)/(POT_VALUE_MAX-POT_VALUE_MIN)*MAX_ANGLE;
     
    }
           
    Stop();//Stop the shaft after the error is acceptable
   
}
};

//DC Motor Class definition completed................//************************************************************//L298 pin defintions.....
int motor_p1=4;
int motor_p2=5;
int pwmPin=9;
DCMotorcustomServo(motor_p1,motor_p2,pwmPin);
//create an instance of the DC motor Class//set the speed at which the motor will turn (Max speed = 255)
int turnSpeed=200;
void setup()
{
 
    //no code is needed here for a simple up and down swing of santa's arm}voidloop(){
 
    //forwards turning..........
 
    for(inti=30;i<=150;i+=15)//our motor will turn from 30 to 150 degrees in steps of 15 degrees
 
    {
   
        customServo.GoToAngle(i,turnSpeed);
     
    }
   
    //backwards turning..........
 
    for( int i=150; i >= 30; i-=15)//now turn from 150 to 30 degrees in steps of 15 degrees
 
    {
   
        customServo.GoToAngle(i,turnSpeed);
     
    }
}

Step 3: Weaving the relay modules with the dancing lights

 

To connect the 4 Relay board to an Arduino is very easy.  

You only need to connect the Arduino +5v to the 4 Relay board VCC pin and an Arduino Ground to the 4 Relay board GND pin.

 

Then it’s a matter of just connecting some data pins from the Arduino to the 4 Relay board IN pins.

 

In the example code below I used Arduino pins 7, 8 , 9, 10.

I didn’t use the same digital pins as the LEDs because they are inversed to each other.  

The LEDs get power from the Arduino, that means when the digital pin is HIGH (1) it has +5v on the line and the LED lights up.

The relay commands are the opposite to this and the relay is switched with the IN pin is set LOW (0).

 

The relays default state is to connect COM to NC (normally closed), this is also done by setting the relays IN pin HIGH.  

This is a safety feature in-case you Arduino looses power it turns off all the relay switches.  

To change the relays state so NO (Normally Open) the pin is set to LOW.

 

I have some 12v DC lights that I want to activate like LEDs as a VU meter.

To do that I connect the lights power line to the 4 Relay board relay Com connector and

the line to the lights connects to the relays NO connector.  The NC connect is left empty.

 

Example of connecting Lights to the relays.

 

 

 

 

Code: Available with purchase

 

 

 

Other Details:

Specification of 4 Relay board:

Equipped with high-current relay, AC250V 10A ; DC30V 10A

Support 5V voltage

Independent wiring in contact area , safe and reliable

Standard interface that can be expanded in a variety of development boards

Equipped with screw holes for easy installation

Applicable to a variety of platforms including   Arduino / AVR / ARM

Size: 7 X 4.9 X1.8 (high) cm

 

Schematic Diagram:

 

 

More information about relays: http://www.kpsec.freeuk.com/components/relay.htm

 

Tips on working with AC power:   … IT IS DANGEROUS !!!...    

Only work with AC if you really know what your doing, it can kill.

 
 

 

Step 2: Interfacing the relay modules to the Arduino

Relays work on electromagnetism, When the Relay coil is energized it acts like a magnet and changes the position of a switch. The circuit which powers the coil is completely isolated from the part which switches ON/OFF, This provides electrical isolation. This is the reason we can control a relay using 5V's from an arduino and the other end of it could be running an 230V appliance, the 230V end is completely isolated from the 5V arduino circuitry.


To connect the 4 Relay board to an Arduino is very easy and allows you to turn on and off an wide range of devices, both AC and DC. The first to connections are the ground and power pins,  You need to connect the Arduino +5v to the 4 Relay board VCC pin and the Arduino ground to the 4 Relay board GND pin.  Then it’s a only a matter of just connecting the communication pins, labeled IN1, IN2, IN3 and IN4, two 4 data pinson the Arduino.In the example code below we used Arduino pins 7, 8 , 9, 10.  Its a good idea to avoid using Data pins 0 and 1 as they are used by the Arduino for serial communication and can cause problems when uploading code to the Arduino.


The default state of the relay when the power is off for COMM (power) to be connected to NC (normally closed), this is the equivalent of setting the 4 Relay boards IN pin to HIGH (has +5v sent to it)  It is a safety feature to notuse the NC connector in-case you Arduino looses power it will automatically turns off all the devices connected to the relay.  When you have something connected to the relays NO (Normally Open) connector and you set the corresponding IN pin to LOW (0v), power will flow in from the COMM connector and out of the NO connectorpowering your device..


Above: Example of connecting power and lights to a relays COM and NO connectors.  

Below: Connecting to the Arduino.


 

// Basic 4 Realy board connection

// Each relay is turned on for 2 seconds and then off.

// You can here them click as there state changes from off to on and on to

// off.

// You will also see the corresponding Red LED on the 4 Relay board

// light up when the relay is on.

 

 //  define names for the 4 Digital pins On the Arduino   

// These data pins link to 4 Relay board pins IN1, IN2, IN3,

IN4

#define RELAY1  6                        

#define RELAY2  7                        

#define RELAY3  8                        

#define RELAY4  9

 

void setup()

{    

// Initialise the Arduino data pins for OUTPUT

  pinMode(RELAY1, OUTPUT);       

  pinMode(RELAY2, OUTPUT);

  pinMode(RELAY3, OUTPUT);

  pinMode(RELAY4, OUTPUT);

}

 

 void loop()

{

   digitalWrite(RELAY1,LOW);           // Turns ON Relays 1

   delay(2000);                                      // Wait 2 seconds

   digitalWrite(RELAY1,HIGH);          // Turns Relay Off

 

   digitalWrite(RELAY2,LOW);           // Turns ON Relays 2

   delay(2000);                                      // Wait 2 seconds

   digitalWrite(RELAY2,HIGH);          // Turns Relay Off

 

   digitalWrite(RELAY3,LOW);           // Turns ON Relays 3

   delay(2000);                                      // Wait 2 seconds

   digitalWrite(RELAY3,HIGH);          // Turns Relay Off

 

   digitalWrite(RELAY4,LOW);           // Turns ON Relays 4

   delay(2000);                                      // Wait 2 seconds

   digitalWrite(RELAY4,HIGH);          // Turns Relay Off        

 }

 

 

 

Previous Step: Making LED lights dance to your music

Next Step: Music Plays and lights dance

Arduino Web Switch : Turn your kettle ON via Internet

Arduino Internet Web Switch

This wonder kit allows you to control any electric appliance from the internet.

What does that mean ?

It means you can turn ON/OFF any appliance at your home from your iphone, Android, ipad, PC or laptop from Anywhere in the world..As long as you have access to a web browser.

Woooo..aint that an amazing power to lay your hands on.


What would I do with a Web Switch ?     

I would do these:

  • Boil water for my coffee right from my bed with my Iphone

  • Turn on my Home heatpump when I leave to home from Work & My home is warm and cozy by the time I arrive :)

  • Open my garage door from my phone without even stepping out of my car

  • Water my garden when I am on vacation

  • Rest I leave to your creative imagination to fill in...


What does the kit consist of ?

  • Arduino UNO
    • This is the CPU where the code and the webserver runs

  • Ethernet shield
    • This shield allows the Arduino to talk to any other device connected via Internet.
    • The shield has an ethernet port which will be later used to connect the ethernet cable.

  • Sensor Shield
    • This shield provides modular plugins to connect loads of things you want to control
    • For this example we will interface a Relay module to control power appliances

  • Relay Module
    • This module allows the Arduino to turn ON/OFF power appliances
    • Read more about relays here

 

Building the Arduino Web Switch 

Stacking the shields

Place the Arduino at the bottom as below

Plug the Ethernet Shield on top of the Arduino ( this is also called stacking , as we keep building a stack of shields above the Arduino )

Once thats done it would look like this.

Next step is to stack the height extender pins into the Ethernet shield ( This is necessary to ensure we dont short circuit the sensor shield pins on to the Ethernet shield )

Once the height extender pins are plugged into the Ethernet shield it will look like the pic below:

Stack the Sensor Shield on top of the Ethernet shield, as shown in pic below.

 

Wiring the Relay Module

Use the cable provided and connect it to the relay module as shown below

Connect the other end to the Sensor shield pin number 8, VCC and GND

TEST: Power up the arduino and you should see the GREEN led on relay module glowing. If you dont see that revisit your wiring and make sure everything is correct.

Arduino IDE ver 1.0.6 setup

Install the IDE version 1.0.6 ( this is the supported version for the examples below ) from the link here:

http://arduino.cc/download.php?f=/arduino-1.0.6-windows.exe

Relay testing

We are about to test if the wiring is good and the relay turns ON and OFF.

Load this program into the arduino from this link.

What does this program do?

This program turn ON the RELAY module for a 3 secs and turns it OFF for another 2 secs and keeps doing that.

How do you know if the relay is turning on? 

You hear a click every time the relay operates and you see the RED led glowing every time it is turned ON.

This test should test if the wiring it good and the arduino is able to control the relay.


Getting your Internet Web Switch to go.......

Now that all the modules are tested, we are almost there..

The last bit which needs doing is to load the Web Switch code and turn your kettle on!

Follow the steps below in order

  • Download Ethernet shield library into libraries folder present in Arduino IDE installation

    • This step adds the library for getting the Ethernet Shield to work

  • Connect a Ethernet cable from the Arduino Ethernet shield to your PC/Laptop 

  • Load this code (link provided on purchase) onto the Arduino.

  • Open up your favorite browser and copy this address onto it:

 

You will see the webpage shown below on the browser. This page is hosted on your Arduino,

yes that tiny little piece of hardware can host a webpage :)

Now you can click the button on the page and hear your relay turn ON/OFF,

You are now able to control the Arduino via the Browser. 

 

Turning the kettle ON!

Now we have a working Arduino Web Switch which can be controlled from a browser,

To control the Kettle you will need to connect the Relay as a switch for the Kettle.

Disclaimer: any modification work that involves high voltages should be done by a certified electrician. This Web Switch kit is provided by Hobbyist Ltd "as is" with no warranty of any kind whatsoever. It is your responsibility to ensure your safety and proper functioning of your devices and applicances. Should you have any concern about health and safety please consult a professional for advice.

The wiring is as below:

 

 

VIDEO of the completed kit in action 

 

NOTE: This kit set and code example is tested and works with the ethernet shield we provide with the kit and does not always work with other off the shelf ethernet shields

Get a WebSwitch for yourself?

 

 

 

 

Booting up the Weather Station

Wiring the Arduino + LCD +  DHT11

First of all, you will need to wire the DHT11 sensor and the LCD as shown in the circuit diagram below:

 

Fig 1: Circuit to wire up the Arduino the LCD and the DHT11 sensor


NOTE: Instread of a fixed resistor show in above diagram a variable resistor is provided for contrast adjustment of the LCD

Loading the program to get the weather station going:

The code for reading the Sensor data and displaying it on the LCD is available (provided on purchase), The code is very easy to read and understand and you can tweak it to your advantage..

Once the code is loaded the Weather Station should be reading the Temperature and Humidity reading and displaying it on the cool blue backlight screen!!!

 

GET A WEATHERSTATION!

NOTE: We leave the interfacing of the light sensor as a task for the hobbyist, But if you get stuck drop us a line and we will send you the link to interface it!

Zircon - This is a contributing Drupal Theme
WeebPal