Hackster is hosting Hackster Holidays, Finale: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Tuesday!Stream Hackster Holidays, Finale on Tuesday!
Robotics EveryDay
Published © CC BY-NC-ND

Ball Balancing Robot | Arduino Object Detection Via OpenCV

This is a ball-balancing robot. It balances the ball in the middle with Object Detection & Image Recognition through Arduino & Python

BeginnerFull instructions provided2,023
Ball Balancing Robot | Arduino Object Detection Via OpenCV

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
SG90 Micro-servo motor
SG90 Micro-servo motor
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Breadboard (generic)
Breadboard (generic)
×1
9V 1A Switching Wall Power Supply
9V 1A Switching Wall Power Supply
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Scissor, Electrician
Scissor, Electrician
Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Custom parts and enclosures

CAD Design Ball Self Balancing Robot

CAD Design Ball Self Balancing Robot

Schematics

Circuit Schematic For Ball Balancing Robot

Circuit Schematic For Ball Balancing Robot

Code

Code for Ball Balancing Robot

Arduino
Code for Ball Balancing Robot
#include <PID_v1.h>
#include<Servo.h>

Servo m;
int t=8,e=9;
int d_mid = 0;

double Setpoint, Input, Output;
//double Kp=0.5, Ki=2.4, Kd=4;
double Kp=0, Ki=0, Kd=400;

PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);

void setup() {
  pinMode(t, OUTPUT);  //trigger pin
  pinMode(e, INPUT);  //echo pin
  m.attach(7);
  
  Input = distance();
  Setpoint = 10;

  myPID.SetMode(AUTOMATIC);

}

int distance(){
  
  float duration, cm;
  
  digitalWrite(t, LOW);
  delayMicroseconds(2);
  digitalWrite(t, HIGH);
  delayMicroseconds(5);
  digitalWrite(t, LOW);
  
  duration = pulseIn(e, HIGH);

  cm = duration/ 29 / 2;
  
  return cm; //returns distance in centimeters
}

void loop() {

  int d = distance();

  
  if (d<22){
    if (d<11){
      myPID.SetOutputLimits(0,90);
      Input = d;
      myPID.Compute();

      m.write(90+Output);
      
    }else{
      myPID.SetOutputLimits(-80,10);
      Input = d-10;
      myPID.Compute();

      m.write(90+Output);
      
    }
  }else{
    m.write(110);  
  }
}

Credits

Robotics EveryDay
3 projects • 9 followers

Comments