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

Sunday, October 14, 2018

Control LED with IR remote arduino

Hello learner, in this post I want to show you how to control LED with IR remote. Here are below the components we required:

- Infrared (IR) remote and receiver
- Arduino board (I was using nano for this tutorial)
- Cable jumpers
- LEDs
- Resistor
- Breadboard

Wiring diagram:

- Blue jumper (IR receiver data) connected to pin 4 arduino), check your own IR device by referring to its datasheet
- Orange jumper (LED 1) connected to pin 2 arduino)
- Green jumper (LED 2) connected to pin 3 arduino)
- Black jumper is Ground
- Red jumper is VCC (5V)


We are going to use 2 arduino programs, first program is for acquiring your remote hexadecimal number and the second program is for testing your IR remote and receiver by controlling the LEDs

*Before uploading the program, we need IR remote library to be able to program this device with arduino, below is the download link for IR remote library. After finish downloading, copy and paste inside your C:\...\...\arduino\library



First program:

#include <IRremote.h>           //IR library

int recPin = 4;                         //Data pin from IR receiver connected to pin 4 arduino
IRrecv recIr(recPin);
decode_results results;

void setup() {
  pinMode(recPin, INPUT);
  Serial.begin(9600);               //Serial communication to get the hexadecimal numbers for each button
  recIr.enableIRIn();
}

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


Second program:

#include <IRremote.h>           //IR library
#define button1 0xFFA25D    //Button no 1 on IR remote with hexadecimal FFA25D
#define button2 0xFF629D    //Button no 2 on IR remote with hexadecimal FF629D

int recPin = 4;                         //Data pin from IR receiver connected to pin 4 arduino
IRrecv recIr(recPin);
decode_results results;

int led1 = 2;                            //Led 1 connected to pin 2 arduino
int led2 = 3;                            //Led 2 connected to pin 3 arduino
boolean stateLed1 = 0;
boolean stateLed2 = 0;

void setup() {
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(recPin, INPUT);
  Serial.begin(9600);             
  recIr.enableIRIn();
}

void loop() {
  if(recIr.decode(&results)){
    Serial.println(results.value, HEX);
    recIr.resume();
    int hexa = results.value;

    switch(hexa){
      case button1:
        {if(stateLed1 == 0){
          digitalWrite(led1, HIGH);
          stateLed1 = 1;
        }
        else if(stateLed1 == 1){
          digitalWrite(led1, LOW);
          stateLed1 = 0;
        }
        }
        break;
        case button2:{
          if(stateLed2 == 0){
            digitalWrite(led2, HIGH);
            stateLed2 = 1;
          }
           else if(stateLed2 == 1){
            digitalWrite(led2, LOW);
            stateLed2 = 0;
           } 
        }
        break;
    
      default:
      Serial.println("waiting request... ... ...");
        }   
    }
}

The Video how to contol LED with IR remote arduino:


If you have a queston, please let me know by put the comment below or on my youtube channel.
See you and thanks.

Friday, October 12, 2018

Arduino Project : How to make automatic fan using arduino and ultrasonic sensor

Hello friends, in this project I’ll show you how to make automatic usb fan using arduino and ultrasonic sensor. So how does it work? The fan will work if the distance is less than 40 cm from the ultrasonic sensor, and when the object gets longer than 40 cm from the ultrasonic sensor, the fan will automatically turn off.
Here are the materials that we need:
  • Arduino board (I use arduino UNO, but any arduino board will work)
  • Ultrasonic sensor HC-SR04
  • Transistor BC548
  • 560 Ohm resistor
  • Breadboard
  • Jumper
  • Power adapter or battery
  • Cardboard
  • USB fan
Here are the connections:
for blog.JPG

Arduino code:
int trig = 7; //ultrasonic trig pin
int echo = 8; //ultrasonic echo pin
byte fan = 6; //mini fan connected to pin 6 arduino uno
long duration;
float distance;
void setup() {
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
pinMode(fan, OUTPUT);
}
void loop() {
digitalWrite(trig, LOW);
delayMicroseconds(3);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
duration = pulseIn(echo, HIGH);
distance = duration/58; //based on ultrasonic datasheet
if(distance < 40){
digitalWrite(fan, HIGH);
}else{
digitalWrite(fan, LOW);
}
}
Youtube Video:

That was it, any doubt, don’t hesitate to ask me on the comments, also please subscribe my youtube channel, Thanks…

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...