Program Servomotor with Arduino  Step by Step Guide ▷ 2020

Program a servo motor with Arduino It is not a difficult task, if you read carefully all the paragraphs of this article. We are going to show you how you can create complex projects without errors.

You will need to know what a servomotor is and what these hardware components are for. We will also explain the uses of these devices and how you can program a servomotor with Arduino.

But this is not all you will see You can also find the best projects of Arduino With servomotor that you can make yourself to practice quickly and easily. Check out.

What is a servomotor and what are these hardware development components for?

What is a servo motor and what are these hardware development components for?

A servomotor It is a special type of motor that is characterized by making precise movements at a certain torque. Its main function is to maintain the position of the axis in a robotic action.

It is composed of a plastic box which contains a arduino board, to which is added a DC type motor, a potentiometer and a gear system. You can find 2 types of servo motors. There are those with limited rotation, which can rotate up to 180 ° and the servo motors with full or continuous rotation. These can have a full 360 ° rotation.

Uses of a servomotor In what type of devices can we use them?

He use of a servomotor is very wide due to the main function it offers in robotics, which is to rotate a part within the same axis.

Therefore, it is common to find a servomotor in:

  • Radio controlled cars in different types of toys and in robots for children.
  • Radars incorporated into different objects.
  • In the automation of manufacturing processes. These robots work in particular in the processes of assembly, automobile construction and in other types of automatic manufacturing at the industrial level.
  • The servo motors can also be found in elevators.
  • While, in computing it is common to find a servomotor in the tray of DVD disc players.

Learn step by step how to program a servomotor with Arduino for your projects from scratch

If you want to program a servomotor with Arduino you will have to follow the steps that we will show you below:

Connect all physical components

The first step you should do is assemble all the components to the Arduino board. To do this you will have to connect three types of cable to the breadboard, one will be the one that will go to ground, the other to the 5 volt supply and the last to the PWM pin. Once you have connected this circuit you must incorporate the servomotor and if you want you can add resistors.

Program the motor rotation

What you should do now is enter the Arduino IDE and write the following commands so that the motor can rotate at three types of angles.

The codes are:

#include <Servo.h>

Servo servoMotor;

void setup() {

begin(9600);

attach(9);

}

void loop() {

Now you must configure the displacement to position 0º, for this you will have to enter:

write(0);

delay(1000);

Lo mismo tendrás que hacer con la posición de 90° y con la de 180°:

write(90);

delay(1000);

write(180);

delay(1000);

}

Turn the motor degree by degree

In this step you will have to incorporate movements so that the servomotor can sweep all degrees from 0 to 180.

For this you will have to enter:

#include <Servo.h>

Servo servoMotor;

void setup() {

Next, you will have to start the serial monitor:

begin(9600);

Then you are going to have to program the servomotor to work from the beginning with pin 9:

attach(9);

write(0);

}

Create loops

At this time you are going to have to generate 2 loops, one negative and one in a positive sense:

void loop() {

for (int i = 0; i <= 180; i++)

{

Then move to the appropriate angle:

write(i);

delay(25);

}

The same you will have to do for the negative sense:

for (int i = 179; i > 0; i--)

{

write(i);

delay(25);

}

}

Best Arduino Projects with Servomotor You Can DIY to Practice

If you want to practice working with a servo motor on an Arduino board, you will have to take these projects into account:

Trash can with motion detector

Trash can with motion detector

This project will allow you to open a trash can by means of a motion sensor. You will need a Micro Arduino board, a generic HC-SR04 type ultrasonic sensor, a servo motor and a waste basket.

For programming you will have to enter these codes:

#include <Servo.h>

Servo myservo ;

const int  servo_pin  =  2 ;

const int  trig_pin  =  3 ;

const int  echo_pin  =  4 ;

const int  inter_time  =  200 ;

int tiempo  =  0 ;

configuración vacía ()

{

Serial . comenzar ( 9600 );

myservo . adjuntar ( servo_pin , 500 ,  2400 );

myservo . escribir ( 90 );

pinMode ( trig_pin ,  SALIDA );

pinMode ( echo_pin ,  ENTRADA );

retraso ( 3000 );

}

bucle vacío ()

{

duración del flotador, distancia;

digitalWrite ( trig_pin , ALTO );

delayMicroseconds ( 1000 );

digitalWrite ( trig_pin , BAJO );

duración =  pulseIn  ( echo_pin ,  HIGH );

distancia =  ( duración / 2 ) / 29 ;

Serial . imprimir ( distancia );

Serial . println ( "cm" );

time = time + inter_time;

delay(inter_time);

if (distance < 10)

{

for(int i = 1500; i >= 1100; i-=25){

writeMicroseconds(i);

println("2");

delay(100);

}

delay(1000);

para ( int i  =  1100 ;  i  <=  1500 ;  i + = 25 ) {

myservo . writeMicroseconds ( i );

Serial . println ( "1" );

retraso ( 100 );

}

}

}

Robot arm

Robot arm

What you will need to build this robotic arm an Arduino UNO board, 3 SparkFun Servo, 1 Module of a two-axis Joystick from a PlayStation2 console and a power supply. You will also have to have on hand wires, a heat sink, a ceramic capacitor and an electrolytic capacitor.

The codes you will have to work with are:

#include "AnalogReader.h"

#include <Arduino.h>

AnalogReader::AnalogReader(const int pin) : m_pin(pin) {}

/**

Read analog value from board ADC

*/

int AnalogReader::read()

{

return analogRead(m_pin);

}

#include "Button.h"

#include <Arduino.h>

Button::Button(const int pin) : m_pin(pin)

{

}

void Button::init()

{

pinMode(m_pin, INPUT);

To set the start state you will have to write:

m_lastButtonState = read();

}

//read button state.

bool Button::read()

{

return digitalRead(m_pin);

}

In case the button is not pressed will return to LOW (0), but if it is triggered it will deliver the value HIGH (1).

For this you have to enter:

bool Button::onChange()

{

//read button state. '1' is pushed, '0' is not pushed.

bool reading = read();

// If the switch changed, due to noise or pressing:

if (reading != m_lastButtonState) {

// reset the debouncing timer

m_lastDebounceTime = millis();

m_pressFlag = 1;

}

if ((millis() - m_lastDebounceTime) > m_debounceDelay) {

// whatever the reading is at, it's been there for longer

// than the debounce delay, so take it as the actual current state:

// if the button state has changed:

if (m_pressFlag) { //reading != m_lastButtonState) {

//update the buton state

m_pressFlag = 0;

// save the reading. Next time through the loop,

m_lastButtonState = reading;

return 1;

}

}

m_lastButtonState = reading;

return 0;

}

bool Button::onPress()

{

//read button state. '1' is pushed, '0' is not pushed.

bool reading = read();

// If the switch changed, due to noise or pressing:

if (reading == HIGH && m_lastButtonState == LOW) {

// reset the debouncing timer

m_lastDebounceTime = millis();

m_pressFlag = 1;

}

if ((millis() - m_lastDebounceTime) > m_debounceDelay) {

if (m_pressFlag) {

After this, we will have to do it through the loop:

m_pressFlag = 0;

m_lastButtonState = reading;

return 1;

}

}

m_lastButtonState = reading;

return 0;

}

bool Button::onRelease()

{

//read button state. '1' is pushed, '0' is not pushed.

bool reading = read();

// If the switch changed, due to noise or pressing:

if (reading == LOW && m_lastButtonState == HIGH) {

// reset the debouncing timer

m_lastDebounceTime = millis();

m_pressFlag = 1;

}

if ((millis() - m_lastDebounceTime) > m_debounceDelay) {

// whatever the reading is at, it's been there for longer

// than the debounce delay, so take it as the actual current state:

// if the button state has changed:

if ( m_pressFlag) { //reading == LOW && m_lastButtonState == HIGH) {

// save the reading. Next time through the loop,

m_lastButtonState = reading;

m_pressFlag = 0;

return 1;

}

}

m_lastButtonState = reading;

return 0;

}

#ifndef _BUTTON_H_

#define _BUTTON_H_

//button class:

class Button {

public:

Button(const int pin);

//initialize button instance

void init();

//Read button state - without debounce

bool read();

//return True on both button events, Press or Release

bool onChange();

//return True only on Press

bool onPress();

//return True only on Release

bool onRelease();

private:

const int m_pin;

bool m_lastButtonState; //state variables

long m_lastDebounceTime = 0;

const int m_debounceDelay = 50;

bool m_pressFlag = 0;

};

#endif //_BUTTON_H_

Insect

Insect

For this project you will need a 3.3V 8MHz Arduino board, a servo motor, a switch, a 1000mAh lithium battery, cables, wires and glue. The assembly must be done holding it as firmly as possible all elements by means of wires and glue.

Then you must enter these codes in the IDE:

#include <Servo.h>

int FRENTE =  4 ;

int BACK =  5 ;

Servo frontLegs, backLegs ;

configuración vacía () {

pinMode ( 13 , SALIDA ) ;

attach ( FRENTE ) ;

attach ( VOLVER ) ;

write ( 90 ) ;

write ( 90 ) ;

retraso ( 5000 ) ;

}bucle vacío () {

digitalWrite ( 13 , ALTA ) ;

write ( 70 ) ;

write ( 70 ) ;

digitalWrite ( 13 , BAJO ) ;

retraso ( 200 ) ;

write ( 110 ) ;

retraso ( 200 ) ;

write ( 110 ) ;

}

If you have any questions, leave them in the comments, we will answer you as soon as possible, and it will also be of great help to more members of the community. Thank you! 😉

You may be interested:

Leave a Comment