Category Archives: Embedded

SERVO MOTOR WITH ARDUINO

In this section, you learn interfacing of servo motor with Arduino, but before this let’s start with servo motor.

A servo motor is an electrical device used to rotate objects at certain specified angles. It’s just a basic motor that runs through the mechanism of the servo.

They are used in numerous applications such as toy cars, robotics, machines etc.

Servo motors are measured in kg / cm (kilogram per centimeter). Servo motors are rated 3kg / cm or 6kg / cm etc. This kg / cm tell you how much weight your servo engine can lift at a given distance. Generally servo motor 9G SG90 is used in projects.

Servo Motor

For example – If the load is suspended 1 cm away from the shaft of the motors, a 3kg / cm Servo motor will be able to lift 3 kg, the greater the distance the less the ability to carry weight. Electrical pulse determines the direction of a servo motor.

COMPONENTS REQUIRED

S.N. COMPONENT QUANTITY
1. Arduino Uno 1
2. Servo Motor SG90 1
3. Jumper wires 3

SERVO MOTOR PINOUT

Servo Motor pinout

WIRE CONFIGURATION

Wire Number Wire Colour Description
1. Brown Ground wire connected to the ground of system
2. Red Powers the motor typically +5V is used
3. Orange PWM signal is given in through this wire to drive the motor

SERVO MOTOR SG90 TECHNICAL FEATURES

    • Operating Voltage is +5V
    • Torque: 2.5kg/cm
    • Operating speed is 0.1s/60°
    • Gear Type: Plastic
    • Rotation : 0°-180°
    • Weight of motor : 9gm
    • Package includes gear horns and screws

HOW SERVO MOTOR WORKS

Servo motors have 3 wires coming out of them. Out of which 2 are used as Power pins (positive and negative) and the third one is used for the signal that is to be sent from the MCU.

All Servo motors operate on the principle of PWM (Pulse Width Modulation), meaning their angle of rotation is controlled by the length of the pulse applied to their Control PIN. Essentially, the servo motor consists of a DC motor powered by a variable resistor (potentiometer – POT) and some gears.

PWM signals of different duty cycles
PWM signals of different duty cycles

Servo motor SG90 can be rotated from 0 to 180 degree, but other servo motors can rotate completely 360 degree depending upon manufacturer. By adding the correct width of the Electrical Pulse to its control pin, this degree of rotation can be controlled.

CIRCUIT DIAGRAM – SERVO MOTOR ARDUINO

To interface servo motor with Arduino make connections like this. PWM supported pins in Arduino Uno are 3, 5, 6, 9, 10, and 11 (~ tilde sign before pin).

Servo motor with Arduino

CONNECTION TABLE

S.N. Arduino Servo Motor
1. +5V VCC (red)
2. GND GND (brown)
3. 9 Signal (orange)

SERVO MOTOR ARDUINO CODE

/*Tutorial: https://pijaeducation.com/servo-motor-with-arduino/
*/
// PWM supported pins in Arduino Uno are 3, 5, 6, 9, 10, and 11.

#define del 15
// include servo library, don't need to install externally already available in arduino IDE
#include <Servo.h>
// create servo object to control a servo
Servo myservo;

void setup() {
  /* Attaches servo variable myservo to pin 9.
    Using myservo you can use servo library inbuilt function, which control servo connected on pin 9
  */
  myservo.attach(9);
  Serial.begin(9600);
}

void loop() {
  // rotate servo motor from 0 to 180 degree
  for (int i = 0 ; i <= 180 ; i++) {
    // set servo motor angle to value i
    myservo.write(i);
    // prints i value to serial monitor
    Serial.println(i);
    delay(del);
  }

  // rotate servo motor from 180 to 0 degree
  for (int i = 180 ; i >= 0 ; i--) {
    myservo.write(i);
    Serial.println(i);
    delay(del);
  }

  // Earlier we attached servo to pin 9, here we can detach it if needed. Not compulsory to do here.
  // myservo.detach();
}

CONNECT MORE SERVO MOTORS

/*Tutorial: https://pijaeducation.com/servo-motor-with-arduino/
*/
// If you want to connect more servo motor then you need to create more servo variables like

#include <Servo.h>
Servo myservo1;
Servo myservo2;

void setup() {
  myservo1.attach(9);
  myservo2.attach(10);
  Serial.begin(9600);
}

OUTPUT

Motor will start rotation from 0 to 180 degree, but these motors are not so accurate, so there are chances the motor can move 15-20 degree less.

Servo motor with Arduino

Total Visits: 2499

DATA TYPE CONVERSION IN ARDUINO

This tutorial covers data type conversion in arduino. Data type conversion or typecasting means converting a value from one data type to other. For example, convert int to float, string to int etc.

Data type covered in this section are int, float, char, char array, string and const char *.

DATA TYPE CONVERSION IN ARDUINO

INT TO OTHER DATA TYPE CONVERSION

This Arduino code will convert data types from int to other, see below

  • Convert int to float in Arduino
  • Convert int to char in Arduino
  • Convert int to char array in Arduino
  • Convert int to String in Arduino
data type conversion in arduino
Data type conversion: Int to float, char, char array[] and String
// https://pijaeducation.com/data-type-conversion-arduino/

#define delcat 2000
#define delsub 1500
#define delexp 1000

int a = 65;
float d;
char f;
char h[5];
String j;

void setup() {
  Serial.begin(9600);
  Serial.println("Defined Variables are -");
  Serial.println("int a = 65;");
  Serial.println("float d;");
  Serial.println("char f;");
  Serial.println("char h[5];");
  Serial.println("String j;");
  Serial.println();


  /* "INT to OTHER"
    Data type conversion*/
  Serial.println("INT to OTHER Data type conversion");
  delay(delcat);

  d = a;
  Serial.print("integer 65 to float : ");
  delay(delexp);  
  Serial.println(d);

  delay(delsub);
  f = a;
  Serial.print("int 65 to char : ");
  delay(delexp);  
  Serial.println(f);

  delay(delsub);
  itoa(a, h, 10); // int, char[], base
  Serial.print("int 65 to char array : ");
  delay(delexp);  
  Serial.print(h[0]);
  Serial.print(" , ");
  delay(delexp);  
  Serial.println(h[1]);

  delay(delsub);
  j = String(a);
  Serial.print("int 65 to String : ");
  delay(delexp);  
  Serial.println(j);
  Serial.println();

}

void loop() {

}

OUTPUT

data type conversion in arduino
Click to Zoom

FLOAT TO OTHER DATA TYPE CONVERSION

This Arduino code will convert data types from float to other, see below

  • Convert float to int in Arduino
  • Convert float to String in Arduino
convert float to
Data type conversion: Float to int and String
// https://pijaeducation.com/data-type-conversion-arduino/

#define delcat 2000
#define delsub 1500
#define delexp 1000

int b;
float c = 1.2;
String j;

void setup() {
  Serial.begin(9600);
  Serial.println("Defined Variables are -");
  Serial.println("int b;");
  Serial.println("float c = 1.2;");
  Serial.println("String j;");
  Serial.println();


  /* "FLOAT to OTHER"
    Data type conversion*/
  Serial.println("FLOAT to OTHER Data type conversion");
  delay(delcat);

  b = int(c);
  Serial.print("float 1.2 to int : ");
  delay(delexp);  
  Serial.println(b);

  delay(delsub);
  j = String(c);
  Serial.print("float 1.2 to String : ");
  delay(delexp);  
  Serial.println(j);
  Serial.println();
}

void loop(){
  
}

OUTPUT

data type conversion in arduino
Click to Zoom

CHAR TO OTHER DATA TYPE CONVERSION

This Arduino code will convert data types from char to other, see below

  • Convert char to int in Arduino
  • Convert char to float in Arduino
  • Convert char to String in Arduino
char to int
Data type conversion: Char to int, float and String
// https://pijaeducation.com/data-type-conversion-arduino/

#define delcat 2000
#define delsub 1500
#define delexp 1000

int b;
float d;
char e = 'a';
String j;

void setup() {
  Serial.begin(9600);
  Serial.println("Defined Variables are -");
  Serial.println("int b;");
  Serial.println("float d;");
  Serial.println("char e = 'a';");
  Serial.println("String j;");
  Serial.println();


  /* "CHAR to OTHER"
    Data type conversion*/
  Serial.println("CHAR to OTHER Data type conversion");
  delay(delcat);

  b = int(e);
  Serial.print("char 'a' to int : ");
  delay(delexp);  
  Serial.println(b);

  delay(delsub);
  d = float(e);
  Serial.print("char 'a' to float : ");
  delay(delexp);  
  Serial.println(d);

  delay(delsub);
  j = String(e);
  Serial.print("char 'a' to String : ");
  delay(delexp);  
  Serial.println(j);
  Serial.println();

}
void loop() {
  // put your main code here, to run repeatedly:

}

OUTPUT

data type conversion in arduino
Click to Zoom

CHAR ARRAY TO OTHER DATA TYPE CONVERSION

This Arduino code will convert data types from char array [] or character array to other, see below

  • Convert char array to int in Arduino
  • Convert char array to String in Arduino
char array to String
Data type conversion: Character array to int and String
// https://pijaeducation.com/data-type-conversion-arduino/

#define delcat 2000
#define delsub 1500
#define delexp 1000

int b;
char g[] = {'1', '2', '3', 'D', 'E'};
String j;

void setup() {
  Serial.begin(9600);
  Serial.println("Defined Variables are -");
  Serial.println("int b;");
  Serial.println("char g[] = {'1', '2', '3', 'D', 'E'};");
  Serial.println("String j;");
  Serial.println();


  /* "CHAR ARRAY to OTHER"
    Data type conversion*/
  Serial.println("CHAR ARRAY to OTHER Data type conversion");
  delay(delcat);

  b = atoi(g);
  Serial.print("char array {'1', '2', '3', 'D', 'E'} to int : ");
  delay(delexp);
  Serial.println(b);

  delay(delsub);
  j = String(g);
  Serial.print("char array {'1', '2', '3', 'D', 'E'} to String : ");
  delay(delexp);
  Serial.println(j);
  Serial.println();


}
void loop() {
  // put your main code here, to run repeatedly:

}

OUTPUT

data type conversion in arduino
Click to Zoom

STRING TO OTHER DATA TYPE CONVERSION

This Arduino code will convert data types from String to other, see below

  • Convert String to int in Arduino
  • Convert String to char array [] (character array) in Arduino
  • Convert String to const char * in Arduino
string to const char *
Data type conversion: String to int, character array[] and const char pointer
// https://pijaeducation.com/data-type-conversion-arduino/

#define delcat 2000
#define delsub 1500
#define delexp 1000

int b;
char h[5];
String i = "92AB1";
const char *k;

void setup() {
  Serial.begin(9600);
  Serial.println("Defined Variables are -");
  Serial.println("int b;");
  Serial.println("char h[5];");
  Serial.println("String i = \"92AB1\";");
  Serial.println("const char *k;");
  Serial.println();


  /* "String to OTHER"
    Data type conversion*/
  Serial.println("String to OTHER Data type conversion");
  delay(delcat);

  b = i.toInt();
  Serial.print("String 92AB1 to Int : ");
  delay(delexp);  
  Serial.println(b);

  delay(delsub);
  i.toCharArray(h, 10); // buffer (variable in which to store), max length of buffer can be
  Serial.print("String 92AB1 to Char Array : ");
  delay(delexp);  
  Serial.println(h);

  delay(delsub);
  k = i.c_str();
  Serial.print("String 92AB1 to Const char* : ");
  delay(delexp);  
  Serial.println(k);
  Serial.println();


}

void loop() {
  // put your main code here, to run repeatedly:

}

OUTPUT

data type conversion in arduino
Click to Zoom

CONST CHAR TO STRING CONVERSION

This Arduino code will convert data types from String to other, see below

  • Convert const char * to String in Arduino
const char * to String
Data type conversion: const char * to String
// https://pijaeducation.com/data-type-conversion-arduino/

#define delcat 2000
#define delsub 1500
#define delexp 1000

String j;
const char *l = "HELLO";

void setup() {
  Serial.begin(9600);
  Serial.println("Defined Variables are -");
  Serial.println("String j;");
  Serial.println("const char *l = \"HELLO\";");
  Serial.println();


  /* "Const char to OTHER"
    Data type conversion*/
  Serial.println("Const char to OTHER Data type conversion");
  delay(delcat);

  j = String(l);
  Serial.print("Const Char to String : ");
  delay(delexp);  
  Serial.println(j);
  Serial.println();

}

void loop() {

}
const char to string in arduino
Click to Zoom

 

 

Total Visits: 10166

8X8 LED MATRIX WITH ARDUINO

In this project you will see how to Interface 8×8 led matrix with Arduino or 8×8 dot led matrix with Arduino. In this section we will connect Arduino and led matrix display and send data wirelessly using Bluetooth HC05.

REQUIRED MATERIAL

S.N.COMPONENTQUANTITY
1.Arduino Uno1
2. 8x8 LED matrix module of 41
3.Bluetooth HC051
4. Jumper wires10 (approx)

MAX7219 DOT LED MATRIX 4 in 1 MODULE

8x8 LED matrix

CIRCUIT DIAGRAM

8X8 LED MATRIX WITH ARDUINO
Click to Zoom

CONNECTION TABLE

ARDUINO BLUETOOTH HC05
+5 V VCC (+5 V)
GND GND
0 (RX) TX
1 (TX) RX

 

ARDUINO 8X8 LED MATRIX
+5 V VCC (+5 V)
GND GND
11 DIN
10 CS
13 CLK

WORKING

After uploading arduino code to board, connect the components as shown in the circuit diagram above then you need to pair your Android phone with Bluetooth HC05 (how to pair) using an Android App (download app).

Once you get paired, then write any message in text box and click over Send button to send data wirelessly, which will appear on LED matrix display board.

As you sent data from your phone it will received by bluetooth HC05 and it transfer data bytes to Arduino Serial pins, where arduino is instructed to read Serial data and display it on 8×8 LED LED Matrix board.

ARDUINO PROGRAMMING CODE

To get run the following code you need to include these Arduino Libraries if not already included.  MD_Parola-3.1.1 (at S.N. 8) and MD_MAX72xx-master (at S.N. 9).

I have FC16 hardware type, you may have other hardware type such as PAROLA_HW, GENERIC_HW, ICSTATION_HW or FC16_HW. Change your hardware type if data in led matrix is not appears properly.

#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>
#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
#define MAX_DEVICES 4

// for NODE MCU
//#define CLK_PIN   D5
//#define DATA_PIN  D7
//#define CS_PIN    D8

// for ARDUINO
#define CLK_PIN   13
#define DATA_PIN  11
#define CS_PIN    10

MD_Parola P = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
uint8_t scrollSpeed = 35;
textEffect_t scrollEffect = PA_SCROLL_LEFT;
textPosition_t scrollAlign = PA_LEFT;
uint16_t scrollPause = 100;
#define  BUF_SIZE  75
char curMessage[BUF_SIZE] = { "" };
char newMessage[BUF_SIZE] = { "PIJA EDUCATION" };
bool newMessageAvailable = true;

void setup() {
  Serial.begin(9600);
  P.begin();
  P.displayText(curMessage, scrollAlign, scrollSpeed, scrollPause, scrollEffect, scrollEffect);
}

void loop() {
  if (P.displayAnimate()) {
    if (newMessageAvailable) {
      strcpy(curMessage, newMessage);
      newMessageAvailable = false;
    }
    P.displayReset();
  }
  if (Serial.available()>0) {
    String st = Serial.readString();
    Serial.println(st);
    st.toCharArray(newMessage, BUF_SIZE);
    newMessageAvailable = true;
  }
}

OUTPUT

Pair android phone with Bluetooth HC05 using Android App. Then write data in text box and click send and your message will get display on 8×8 dot matrix led display.

MAX7219 8x8 Dot LED MATRIX

 

Total Visits: 2451

ROBOT CAR APK

This section tells you, how to use ‘Robot Car App’, designed for Bluetooth control robot car using Arduino and to receive or transmit serial data from Arduino (via Bluetooth HC05 or HC06). Download Robot car apk.

ROBOT CAR APK

Download and Install latest version of Robot car apk. and follow these steps to configure your phone with app.

1. Turn on your android phone Bluetooth and search for the available Bluetooth devices. There you find either HC05 or any other depends on which one you are using.

2. Now enter default password 1234 or 0000 to pair with the Bluetooth HC05 (General method to connect with any other BT device).

Bluetooth HC05 Pairing
Click to Zoom

3. After successfully pairing your phone with HC05, Now open Robo Car App. You might see this screen, Just click on screen other than dialogue box.

Robot car
Click to Zoom

4. Now sets commands like ‘f’ for forward and ‘b’ for backward etc. and click SAVE.

control bluetooth robot car instruction
Click to Zoom

5. Click on “Available BT Devices” and you will see all paired bluetooth devices there, select your BT device you want to connect to. Here I want to connect to HC-06.

robot car apk

6. Now you can click over forward, backward, right and left arrow keys to control your signal. Where forwards sends ‘f’,  backward sends ‘b’, right sends ‘r’ and left sends ‘l’.

robot car apk
Click to zoom

7. Other than forward, backward you can send custom messages as well. Write your message in text box and click SEND. As well as in this text box you will see all Serial print data.

Serial data bluetooth
Click to Zoom

8. Now if you want you can disconnect your phone with bluetooth HC05 by clicking on DISCONNECT button

connect mobile with hc05
Click to zoom

 

ALSO READ: BLUETOOTH CONTROLLED ROBOT CAR USING ARDUINO

Total Visits: 1305