Happy ebotics dog – Learning path project 9

Our Learning Path is an itinerary designed to teach all the foundaments and a little more about electronics, coding and robotics.

When you complete all the projects you will be able to develop almost any project you can imagine. In each chapter new concepts are introduced. You will learn by doing in a funny way.

Build this funny dog, always happy when you are near him!

Project 9
HAPPY EBOTICS DOG

Learn to include servomotors in your projects with this funny dog. Always happy when something is near!

BEFORE TO START

DIFFICULTY LEVEL: Intermediate
DURATION OF THE ACTIVITY: 45 min.

MATERIALS:
4in1 board
Buzzer
Battery holder & wires
Battery holder & wires
Battery holder & wires

Battery holder, USB cable and wires.

9 - Servomotor 180º

The servomotor from ebotics it's a tipical micro servo with movement from 0 to 180ª, half turn, and reverse. We can control the exact angle in this range from our program.

Internally a servo frequently consists of a reducing mechanism. Therefore they provide a high torque and a high degree of precision. On the other hand, the turning speeds are small compared to direct current motors.

PWM and servomotors

Do you remember when used PWM to control led brightness? Servos also need PWM to control their position!

9. Servomotor - Circuit

To get a PWM signal we use any digital port of 4in1 board as output, so we need one digital output to control the servo. In next exercises we will use an analog sensor, and the ultrasonic one which need two ports as you know.

Connect wires from 4in1 board to components:
- DIO6 to Servomotor module
- DIO5 to LED module
- DIO2 to ultrasound trigger
- DIO3 to ultrasound echo
- A1 to potentiometer

9.1 Servomotor at 0º 90º and 180º - Program

In Arduino IDE we use a library included by default to easily work with servos: servo.h.
In MBlocks there is also a dedicated block for servos.

Block Coding
Arduino Coding
#include <Servo.h>

Servo dog; //Object from servo motor

void setup() {
  dog.attach(6); // pin from servo motor
}

void loop() {
  dog.write(0); // set the servo motor 0ª
  delay(1000);  // wait for 1 second
  dog.write(90);  // set the servo motor 90ª  
  delay(1000);  // wait for 1 second
  dog.write(180); // set the servo motor 180ª
  delay(1000);  // wait  for 1 second
}
9.2 Servo position by potentiometer

Read the analog sensor value and set the servo to according position. Remeber value ranges of each one! We will use the map function.

Block coding
Arduino code
#include <Servo.h>

Servo dog; //Object from servo motor
int valor = 0;

void setup() {
  dog.attach(6); // pin from servo motor
}

void loop() {
  valor = analogRead(1);
  valor = map(valor, 2023, 0, 0, 180);
  // control the servo motor with potentiometer:
  dog.write(valor);
  delay(200);
}
9.3 Move when object detected less than 20cm

We will introduce the NewPing library for Arduino IDE wich has some interesting funcions to work with HCSR04 ultrasonic sensor used in ebotics module.

Block coding
Arduino code
#include <NewPing.h> // include the libery 
#include <HCSR04.h>
#include <Servo.h>

#define TRIG  2
#define ECHO  3 
#define DISTANCE 400

NewPing sonar(TRIG, ECHO, DISTANCE);

float distance;

Servo dog;
int valor;

void setup() {
    dog.attach(6);
}

void loop() {
  distance = sonar.ping_cm();
  if(distance < 20){
    dog.write(0);
    delay(200);
    dog.write(180);
    delay(200);
  }else{
    dog.write(0);
    delay(200);
  }
}

You can try to use another sensor, for example, move the servo when somebody or some animal is detected with de PIR sensor!!

Final project - Happy ebotics dog

We encourage you to build this dog that will always be glad that you approach him. Here cardboard template

Block coding
Arduino code
#include <Servo.h>
Servo motor1; // Servomotor variable
int RedLED = 4, YellowLED = 5;
int Trigger = 13;       // Ultrasonic sensor Trigger pin to digital pin 13
int Echo = 12;         // Ultrasonic sensor Trigger pin to digital pin 12
float distance;         // variable to measure distance
float vsound=34000.0;  // sound velocity = 340 m/s, in cm 

void setup() {
  // put your setup code here, to run once:
  motor1.attach (9); //  Servomotor digital pin 9
  pinMode(RedLED, OUTPUT); // RED LED configuration to output
  pinMode(YellowLED, OUTPUT); // RED LED configuration to output
  pinMode(Trigger,OUTPUT);        // Trigger configuration to output
  pinMode(Echo,INPUT);            // Echo configuration to output
}

void loop() {
  // put your main code here, to run repeatedly:
  
  //Funcion to distance calculate
  digitalWrite(Trigger,HIGH);     //ON Trigger state and wait 10 us
  delayMicroseconds(10);
  digitalWrite(Trigger,LOW);      //OFF Trigger state

  unsigned long time= pulseIn(Echo,HIGH);   // We get the pulse width
  distance=(time*0.000001*vsound)/2.0;    //Move from microseconds to seconds, multiplying by 0.000001
  
  // if Distance is minor to 30 cm
  if (distance < 30) 
    {
      // move the tail and the LED flashes 2 times
      for (int x=0; x<2 ; x++)
      {
        motor1.write (140); 
        digitalWrite(RedLED, HIGH);
        digitalWrite(YellowLED, LOW);
        delay (300);
        motor1.write (60); 
        digitalWrite(RedLED, LOW);
        digitalWrite(YellowLED, HIGH);
        delay (300);
      }    
    }
    else
    {
      // Tail in initial position and LED off
      motor1.write(140);  
      digitalWrite(RedLED, HIGH);
      digitalWrite(YellowLED, HIGH);
    }
}
 
}
previous arrow
next arrow
Slider

The Mega Maker Kit fits perfectly with the Learning Path, you can build all projects with it, but if you have other kits, you can also follow the entire itinerary and finish some projects, or buy the missing components. You can check in our Learning Path page.

0 0

0