Thursday, October 25, 2018

How to control servo motor with potentiometer and ultrasonic sensor HC-SR04

Hello,

In this post I want to share how to control servo motor with potentiometer and ultrasonic sensor HC-SR04. Lets get started.

Schematic

- Schematic for servo motor + potentiometer














- Schematic for servo + Ultrasonic sensor HC-SR04



Program
- Program for servo motor + potentiometer

#include <Servo.h>

Servo myservo;

int pot = A0;                                            // Potensiometer ke pin A0 arduino
int nilai_analog;
int nilai_mapping;

void setup() {
  myservo.attach(3);                                // Servo ke pin 3 arduino
  pinMode(pot, INPUT);
}

void loop() {
   nilai_analog = analogRead(pot);
   nilai_mapping = map(nilai_analog, 0, 1023, 0, 180);
   myservo.write(nilai_mapping);
 
}


- Program for servo motor + ultrasonic sensor HC-SR04

#include <Servo.h>

Servo myservo;

const byte trig = 7;
const byte receive = 8;

unsigned long waktu;
float jarak;
int nilai_mapping;

void setup() {

  pinMode(trig, OUTPUT);
  pinMode(receive, INPUT);
  myservo.attach(3);
  Serial.begin(9600);
}

void loop() {
  digitalWrite (trig, LOW);
  delayMicroseconds(3);
  digitalWrite (trig, HIGH);
  delayMicroseconds(10);
  digitalWrite (trig, LOW);

    waktu = pulseIn(receive, HIGH);
    jarak = waktu/58;
    Serial.println(jarak);
    delay(100);
      nilai_mapping = map(jarak, 0, 20, 0, 180);
      myservo.write(nilai_mapping);
   
}


Video
- For video please click link below.


I hope it will help somebody :)
Thanks

5 comments:

  1. Kang kok saya salah di waktu=pulseln(receive,HIGH);

    Tulisannya
    servo_dan_sensor_jarak:30:29: error: 'pulseln' was not declared in this scope


    Mohon pencerahan nya master..

    ReplyDelete
  2. Itu buakn pulseln, tapi pulseIn

    ReplyDelete
  3. 'AO' was not declared in this scope ni mksd ny apa bang

    ReplyDelete
  4. mantap bang jadi mau coba bang

    ReplyDelete

How to control servo motor with potentiometer and ultrasonic sensor HC-SR04

Hello, In this post I want to share how to control servo motor with potentiometer and ultrasonic sensor HC-SR04. Lets get started. Schem...