software

How to program a servomotor with arduino to create complex projects? Step by step guide

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

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

But this is not all you will see, you can also find the best Arduino projects with a servo motor that you can do yourself to practice quickly and easily. Check out.

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

A servomotor 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 an 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 full or continuous rotation servomotors. These can have a full 360 ° rotation.

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

The 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 servo motor 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 assembly processes, automobile construction and other types of automated manufacturing at the industrial level.
  • The servomotors can also be found in elevators .
  • Whereas, in computing it is common to find a servo motor in the tray of DVD disc players .

Learn step by step how to program a servo motor 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 will have to do is to assemble all the components to the Arduino board . For 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 in three types of angles.

The codes are:

#include <Servo.h>

Servo servo motor;

void setup () {

begin (9600);

attach (9);

}

void loop () {

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

write (0);

delay (1000);

You will have to do the same with the 90 ° position and with the 180 ° position:

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 servo motor;

void setup () {

Next, you will have to start the serial monitor:

begin (9600);

Then you will 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 with a work with a servo motor on an Arduino board, you will have to take these projects into account:

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 trash can.

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 time = 0;

empty setting ()

{

Serial. begin (9600);

myservo. attach (servo_pin, 500, 2400);

myservo. write (90);

pinMode (trig_pin, OUTPUT);

pinMode (echo_pin, INPUT);

delay (3000);

}

empty loop ()

{

float duration, distance;

digitalWrite (trig_pin, HIGH);

delayMicroseconds (1000);

digitalWrite (trig_pin, LOW);

duration = pulseIn (echo_pin, HIGH);

distance = (duration / 2) / 29;

Serial. print (distance);

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);

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

myservo. writeMicroseconds (i);

Serial. println ("1");

delay (100);

}

}

}

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 need to have cables, a heat sink, a ceramic capacitor, and an electrolytic capacitor on hand.

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);

}

If the button is not pressed, it will return to LOW (0), but if it is activated 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

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 by holding all the elements as firmly as possible by means of the wires and glue.

Then you must enter these codes in the IDE:

#include <Servo.h>

int FRONT = 4;

int BACK = 5;

Servo frontLegs, backLegs;

empty setting () {

pinMode (13, OUTPUT);

attach (FRONT);

attach (BACK);

write (90);

write (90);

delay (5000);

} empty loop () {

digitalWrite (13, HIGH);

write (70);

write (70);

digitalWrite (13, LOW);

delay (200);

write (110);

delay (200);

write (110);

}

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button