Arduino - Servo Motor Tutorial

In this Project, we will see about how to interface Servo Motor with Arduino.

Circuit Diagram:


Arduino Code:
servo_knob_code.ino
#include <Servo.h>
Servo myservo;

int potpin = 0;
int val;

void setup() {
  myservo.attach(9);
}

void loop() {
  val = analogRead(potpin);
  val = map(val, 0, 1023, 0, 180);
  myservo.write(val);
  delay(15);
}

servo_90_code.ino
#include <Servo.h>

Servo myservo;

void setup() {
  myservo.attach(9);
}

void loop() {
  myservo.write(0);
  delay(1000);
  myservo.write(90);
  delay(1000);
}

servo_sweep_code.ino
#include <Servo.h>
Servo myservo;

int pos = 0;

void setup() {
  myservo.attach(9);
}

void loop() {
  for (pos = 0; pos <= 180; pos += 1) {
    myservo.write(pos);
    delay(15);
  }
  for (pos = 180; pos >= 0; pos -= 1) {
    myservo.write(pos);
    delay(15);
  }
}

Youtube Video Tutorial:




Download our official Android App in PlayStore. Click Here
You can get the all the required files (like Circuit Diagram, Arduino.ino file, Libraries Used, and others) for the project  in ZIP format and much more...

IR Receiver Arduino

In this Project, we will see about how to interface IR Receiver TSOP 1838 with Arduino.

Circuit Diagram:


Arduino Code:
simple_led_control_code.ino
//This Code is Developed by Sdev
//Follow Us Here : https://youtube.com/sdevelectronics

#include <IRremote.h>

int RECV_PIN = 11;

IRrecv irrecv(RECV_PIN);

decode_results results;

void setup()
{
  Serial.begin(9600);
  Serial.println("Enabling IRin");
  irrecv.enableIRIn(); // Start the receiver
  Serial.println("Enabled IRin");

  pinMode(3, OUTPUT);
}

void loop() {
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);

    if (results.value == 0x80BF21DE) {  //on
      digitalWrite(3, HIGH);
    }
    if (results.value == 0x80BF916E) {  //OFF
      digitalWrite(3, LOW);
    }
    irrecv.resume(); // Receive the next value
  }
  delay(100);
}

rgb_led_control_code.ino
//This Code is Developed by Sdev
//Follow Us Here : https://youtube.com/sdevelectronics

#include <IRremote.h>

#define RED 9
#define GREEN 10
#define BLUE 11

int RECV_PIN = 8;
IRrecv irrecv(RECV_PIN);
decode_results results;

void setup() {
  Serial.begin(9600);
  Serial.println("Enabling IRin");
  irrecv.enableIRIn();
  Serial.println("Enabled IRin");

  pinMode(3, OUTPUT);
}

void loop() {
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);

    if (results.value == 0x80BF916E) {  //RED
      analogWrite(RED, 255);
      analogWrite(GREEN, 0);
      analogWrite(BLUE, 0);
    }
    if (results.value == 0x80BF21DE) {  //GREEN
      analogWrite(RED, 0);
      analogWrite(GREEN, 255);
      analogWrite(BLUE, 0);
    }

    if (results.value == 0x80BF9B64) {  //YELLOW
      analogWrite(RED, 128);
      analogWrite(GREEN, 128);
      analogWrite(BLUE, 0);
    }

    if (results.value == 0x80BF6996) {  //BLUE
      analogWrite(RED, 0);
      analogWrite(GREEN, 0);
      analogWrite(BLUE, 255);
    }
    if (results.value == 0x80BF3BC4) {  //OFF
      analogWrite(RED, 0);
      analogWrite(GREEN, 0);
      analogWrite(BLUE, 0);
    }
    irrecv.resume(); // Receive the next value
  }
  delay(100);
}

Libraries Used:

Youtube Video Tutorial:




Download our official Android App in PlayStore. Click Here
You can get the all the required files (like Circuit Diagram, Arduino.ino file, Libraries Used, and others) for the project  in ZIP format and much more...

Ultrasonic Sensor Arduino

In this Project, we will see about how to interface Ultrasonic Sensor with Arduino .

Circuit Diagram:

Arduino Code:
ultrasonic_simple_code.ino
//This Code is Developed by Sdev
//Follow Us Here : https://youtube.com/sdevelectronics

#define echoPin 13
#define trigPin 12

long duration, distance;

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  duration = pulseIn(echoPin, HIGH);
  distance = duration / 58.2;
  Serial.println(distance);
  delay(100);
}

ultrasonic_condition_code.ino
//This Code is Developed by Sdev
//Follow Us Here : https://youtube.com/sdevelectronics

#define echoPin 13
#define trigPin 12
#define red 8
#define green 7

long duration, distance;

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(echoPin, INPUT);
}
void loop() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  duration = pulseIn(echoPin, HIGH);
  distance = duration / 58.2;
  Serial.println(distance);
  if (distance < 10) {
    digitalWrite(red, HIGH);
    digitalWrite(green, LOW);
  }
  else {
    digitalWrite(red, LOW);
    digitalWrite(green, HIGH);
  }
  delay(100);
}

Youtube Video Tutorial:




Download our official Android App in PlayStore. Click Here
You can get the all the required files (like Circuit Diagram, Arduino.ino file, Libraries Used, and others) for the project  in ZIP format and much more...

TFT Calculator with Arduino

In this Project, we will see about how to make Calculator with Arduino & TFT Touch Display.

Circuit Diagram:
Just Install TFT Touch Display Shield to Arduino

Arduino Code:
tft_calculator_code.ino
#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
#include <TouchScreen.h>

const int XP = 7, XM = A1, YP = A2, YM = 6; //ID=0x7789

#define BLACK   0x0000
#define RED     0xF800
#define CYAN    0x07FF
#define PINK    0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF
#define MINPRESSURE 10
#define MAXPRESSURE 1000

#define TS_MINX 199
#define TS_MINY 947
#define TS_MAXX 913
#define TS_MAXY 191

TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300); //300 is the sensitivity

String symbol[4][4] = {
  { "7", "8", "9", "/" },
  { "4", "5", "6", "*" },
  { "1", "2", "3", "-" },
  { "C", "0", "=", "+" }
};
int X, Y;
long Num1, Num2, Number;
char action;
boolean result = false;

void setup() {
  Serial.begin(9600); //Use serial monitor for debugging
  tft.reset();
  tft.begin(0x7789); //Change ID, it varies with other TFT Displays
  tft.setRotation(0); // Portrait
  tft.fillScreen(BLACK);

  IntroScreen();
  delay(3000);
  draw_BoxNButtons();
}

void loop() {
  TSPoint p = waitTouch();
  Y = p.y; X = p.x;

  DetectButtons();
  if (result == true)
    CalculateResult();
  DisplayResult();
  delay(300);
}

TSPoint waitTouch() {
  TSPoint p;
  do {
    p = ts.getPoint();
    pinMode(XM, OUTPUT);
    pinMode(YP, OUTPUT);
  } while ((p.z < MINPRESSURE ) || (p.z > MAXPRESSURE));
  p.x = map(p.x, TS_MINX, TS_MAXX, 0, 240);
  p.y = map(p.y, TS_MINY, TS_MAXY, 0, 320);;
  return p;
}

void DetectButtons() {
  if (X < 60 && X > 0) { //Detecting Buttons on Column 1
    if (Y > 260 && Y < 320) { //If cancel Button is pressed
      Serial.println ("Button Cancel");
      Number = Num1 = Num2 = 0;
      result = false;
    }

    if (Y > 200 && Y < 260) { //If Button 1 is pressed
      Serial.println ("Button 1");
      if (Number == 0)
        Number = 1;
      else
        Number = (Number * 10) + 1; //Pressed twice
    }

    if (Y > 140 && Y < 200) { //If Button 4 is pressed
      Serial.println ("Button 4");
      if (Number == 0)
        Number = 4;
      else
        Number = (Number * 10) + 4; //Pressed twice
    }

    if (Y > 80 && Y < 140) { //If Button 7 is pressed
      Serial.println ("Button 7");
      if (Number == 0)
        Number = 7;
      else
        Number = (Number * 10) + 7; //Pressed twice
    }
  }

  if (X < 120 && X > 60) { //Detecting Buttons on Column 2
    if (Y > 260 && Y < 320) {
      Serial.println ("Button 0"); //Button 0 is Pressed
      if (Number == 0)
        Number = 0;
      else
        Number = (Number * 10) + 0; //Pressed twice
    }

    if (Y > 200 && Y < 260) {
      Serial.println ("Button 2");
      if (Number == 0)
        Number = 2;
      else
        Number = (Number * 10) + 2; //Pressed twice
    }

    if (Y > 140 && Y < 200) {
      Serial.println ("Button 5");
      if (Number == 0)
        Number = 5;
      else
        Number = (Number * 10) + 5; //Pressed twic
    }

    if (Y > 80 && Y < 140) {
      Serial.println ("Button 8");
      if (Number == 0)
        Number = 8;
      else
        Number = (Number * 10) + 8; //Pressed twic
    }
  }

  if (X < 180 && X > 120) { //Detecting Buttons on Column 3
    if (Y > 260 && Y < 320) {
      Serial.println ("Button Equal");
      Num2 = Number;
      result = true;
    }

    if (Y > 200 && Y < 260) {
      Serial.println ("Button 3");
      if (Number == 0)
        Number = 3;
      else
        Number = (Number * 10) + 3; //Pressed twice
    }

    if (Y > 140 && Y < 200) {
      Serial.println ("Button 6");
      if (Number == 0)
        Number = 6;
      else
        Number = (Number * 10) + 6; //Pressed twice
    }

    if (Y > 80 && Y < 140) {
      Serial.println ("Button 9");
      if (Number == 0)
        Number = 9;
      else
        Number = (Number * 10) + 9; //Pressed twice
    }
  }

  if (X < 240 && X > 180) { //Detecting Buttons on Column 3
    Num1 = Number;
    Number = 0;
    tft.setCursor(200, 20);
    tft.setTextColor(CYAN);
    if (Y > 260 && Y < 320) {
      Serial.println ("Addition");
      action = 1;
      tft.println('+');
    }
    if (Y > 200 && Y < 260) {
      Serial.println ("Subtraction");
      action = 2;
      tft.println('-');
    }
    if (Y > 140 && Y < 200) {
      Serial.println ("Multiplication");
      action = 3;
      tft.println('*');
    }
    if (Y > 80 && Y < 140) {
      Serial.println ("Division");
      action = 4;
      tft.println('/');
    }
    delay(300);
  }
}

void CalculateResult() {
  if (action == 1)
    Number = Num1 + Num2;
  if (action == 2)
    Number = Num1 - Num2;
  if (action == 3)
    Number = Num1 * Num2;
  if (action == 4)
    Number = Num1 / Num2;
}

void DisplayResult() {
  tft.fillRect(0, 0, 240, 80, RED);  //clear result box
  tft.setCursor(10, 20);
  tft.setTextSize(4);
  tft.setTextColor(WHITE);
  tft.println(Number); //update new value
}

void IntroScreen() {
  tft.setCursor (55, 90);
  tft.setTextSize (3);
  tft.setTextColor(YELLOW);
  tft.println("ARDUINO");
  tft.setCursor (30, 130);
  tft.println("CALCULATOR");

  tft.setCursor (60, 190);
  tft.setTextSize (2);
  tft.setTextColor(WHITE);
  tft.println("modified by");

  tft.setCursor (40, 220);
  tft.setTextSize (4);
  tft.setTextColor(CYAN);
  tft.println("STAYSAY");
}

void draw_BoxNButtons() {
  //Draw the Result Box
  tft.fillRect(0, 0, 240, 80, RED);

  //Draw First Column
  tft.fillRect  (0, 260, 60, 60, CYAN);      //c
  tft.fillRect  (0, 200, 60, 60, WHITE);    //1
  tft.fillRect  (0, 140, 60, 60, WHITE);    //4
  tft.fillRect  (0, 80, 60, 60, WHITE);     //7

  //Draw Third Column
  tft.fillRect  (120, 260, 60, 60, PINK);    //=
  tft.fillRect  (120, 200, 60, 60, WHITE);    //3
  tft.fillRect  (120, 140, 60, 60, WHITE);    //6
  tft.fillRect  (120, 80, 60, 60, WHITE);     //9

  //Draw Secound & Fourth Column
  for (int b = 260; b >= 80; b -= 60) {
    tft.fillRect  (180, b, 60, 60, YELLOW);   //4th column
    tft.fillRect  (60, b, 60, 60, WHITE);     //2nd column
  }

  //Draw Horizontal Lines
  for (int h = 80; h <= 320; h += 60)
    tft.drawFastHLine(0, h, 240, BLACK);

  //Draw Vertical Lines
  for (int v = 0; v <= 240; v += 60)
    tft.drawFastVLine(v, 80, 240, BLACK);

  //Display keypad lables
  for (int j = 0; j < 4; j++) {
    for (int i = 0; i < 4; i++) {
      tft.setCursor(22 + (60 * i), 100 + (60 * j));
      tft.setTextSize(3);
      tft.setTextColor(BLACK);
      tft.println(symbol[j][i]);
    }
  }
}

Libraries Used:

Youtube Video Tutorial:




Download our official Android App in PlayStore. Click Here
You can get the all the required files (like Circuit Diagram, Arduino.ino file, Libraries Used, and others) for the project  in ZIP format and much more...