Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
Leesounghoongledel
Published © GPL3+

arduino(4-2) (FabLab)

I learn how to use Servo motor and I made the code to use servo motor

BeginnerProtip15 minutes1,442
arduino(4-2) (FabLab)

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Servos (Tower Pro MG996R)
×1
LED (generic)
LED (generic)
×1
Resistor 330 ohm
Resistor 330 ohm
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

servo motor and ultra sonic sensor

This circuit is using an ultrasonic sensor and servo motor

Code

servo motor and ultra sonic sensor

Arduino
this code is that If the measured value is less than 30, the servo motor rotates 30 degrees.
#include <Servo.h>
Servo servo;
int trigPin = 9;
int echoPin = 8;
long distance;
long duration;

void setup() {
  Serial.begin(9600);
servo.attach(7);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
  Serial.print("I'm watting");
}

void loop() {
  ultra();
  Serial.print(" 거리 :");
  Serial.println(distance);
  delay(50);
if(distance <= 30){
  analogWrite(12,250);
servo.write(30);
delay(500);
servo.write(0);
delay(500);
analogWrite(12,0);
}
analogWrite(13,250);
delay(500);
analogWrite(13,0);
servo.write(0);
delay(500);

}
void ultra(){
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

// Calculating the distance
distance= duration*0.034/2;
}

Credits

Leesounghoon
9 projects • 4 followers
Hi my name is sung hoon lee. I`m a student at Bongilcheon High School. I want to learn 3D Printing and how to use arduino code(+python code)
Contact
gledel
100 projects • 116 followers
Looking back on my childhood, I was happy when I was making something and I was proud of myself. "Making is instinct!"
Contact

Comments

Please log in or sign up to comment.