Introduction: Autocharge SG90 Nano Robot

About: Argentine writer and inventor

Usually an indoor navigation is based on BT beacons or other expensive equipment. The main idea of this project was to develop a cheap robot (Arduino Nano and 2 SG90 servos), capable of navigating for certain time and then try to find the charging base.

How does it work?

The robot will move around avoiding obstacles due to the distance sensor. It will also maintains a configurable running time counter. When that counter decrease under a certain level, the robot will enter "charging mode"

In "charging mode", it will use the digital compass module to go towards certain coordinates (example: all the way south east) to find the charging station.

When the robot enters charging station, the line tracking sensor will detect a color change and the robot will stay there for certain time to allow charging.

Charging base is a simple device with - connected to an aluminium base and + connected to a high pole. The robot will make contact using an antenna for the + and a ground cable to make contact with aluminium.


1 x Arduino Nano

2 x Sg90 Servo continuous rotation (IMPORTANT: not the regular SG90 but the 360 continuous)

1 x 3,7 1000ma battery

1 x Switch

1 x Distance sensor HC-05

1 x 1 digital compass QMC5883

1 x TP 4056 charger

For the charging base: a 5v Power Supply, wires and an aluminium base.

Step 1: Front Axis

Just glue both servo motors together. You can also cut the jumpers, connect both + (red wire), both - (brown wire) and use a new jumper connector for servo signals (orange wires)

Step 2: 3D Print Robot Body (or Make It With Cardboard and Glue)

If you have a 3d printer, you can just print wheels, the support body and the front sensor case. Files can be found here.

But if you don't, just glue the battery to the front axis and use use a stick to place the line tracking sensor.

Note: In the pictures you will find a bluetooth receiver since this robot is also capable of remote BT controlling.

Step 3: Circuit

  • All 5v/3v from sensors and motors are connected together All GND are connected together
  • Then battery out from TP4056 is connected to Arduino VIN (switch in the middle) Battery output - from TP4056 - to Arduino GND
  • Servo motor 1, orange cable to D9
  • Servo motor 2, orange cable to D10
  • Distance sensor trig pin to D6
  • Distance sensor echo pin to D7
  • Line to D8
  • Compass SDA to A4
  • Compass SCL to A5

Note: The circuit image includes some parts that are optional for this project like BlueTooth module, laser and light detection.

Step 4: Main Code

// SG90 Robot // Obstacle avoidance and navigation #include #define WHEEL_RIGHT 9 // right #define WHEEL_LEFT 10 // left int pulsoMin = 160; int pulsoMax = 1850; // distance sensor #define trigPin 6 #define echoPin 7 long duration; int distance; int distanceLimit= 20; // Servo Wheels Servo servoRight; Servo servoLeft; int compensateWheel=2; void setup() { // attach servo Wheels servoRight.attach(WHEEL_RIGHT, pulsoMin, pulsoMax); servoLeft.attach(WHEEL_LEFT, pulsoMin, pulsoMax); // Distance sensor pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); // on board led pinMode(13, OUTPUT); Serial.begin(9600); Serial.println("Obstacle avoidance started"); Serial.println(""); } void loop() { distance=checkDistance(); // Check for obstacles if (distance>distanceLimit){ robotForward(5); } else { robotTurnLeft(250); }// obstacle detected }// loop void robotForward(int howLong){ Serial.println("Forward"); servoRight.write(0); servoLeft.write(180-compensateWheel); delay(howLong); } void robotBack(int howLong){ Serial.println("Forward"); servoRight.write(180); servoLeft.write(0+compensateWheel); delay(howLong); } void robotStop(int howLong){ Serial.println("Stop"); servoRight.write(90); servoLeft.write(90); delay(howLong); } void robotTurnLeft(int howLong){ Serial.println("Turn"); servoRight.write(0); servoLeft.write(0); delay(howLong); } void robotTurnRight(int howLong){ Serial.println("Turn"); servoRight.write(180); servoLeft.write(180); delay(howLong); } int checkDistance(){ // Clear the trigPin digitalWrite(trigPin, LOW); delayMicroseconds(5); // Trigger the sensor digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Read the echoPin duration = pulseIn(echoPin, HIGH); // Calculate the distance distance= duration*0.034/2; Serial.print(""); Serial.print("Distance: "); Serial.print(distance); Serial.println(); return distance; }

Step 5: Compass Navigation to Base Code

int x,y,z;,&y,&z); angle = atan2(x, y)/0.0174532925; if(angle < 0) angle+=360; angle = 360-angle; Serial.println(angle); // correct angle forward if (angle > 158 && angle < 203) { Serial.println("Go"); distance=checkDistance(); // Check for obstacles if (distance>distanceLimit){ robotForward(5); } else { robotTurnLeft(250); }// obstacle detected } // end right angle else{ do { robotTurnLeft(10);,&y,&z); angle = atan2(x, y)/0.0174532925; if(angle < 0) angle+=360; angle = 360-angle; delay(50); } while (angle<159 or angle>202); }// end else

Step 6: Detect Base Code

// line sensor reading sensorValue = digitalRead (sensorPin); if (sensorValue==1){ Serial.println("Found base"); // charging now robotForward(500); digitalWrite(LED_BUILTIN, HIGH); robotStop(50000); robotBack(3000); digitalWrite(LED_BUILTIN, LOW); robotTurnRight(500); robotForward(1); }

Step 7: Charging Base

In the picture you can find a reference for charging base. A 5V power supply with Ground connected to an aluminium base and 5V + connected to an upper pole. Those 2 will make contact with robot TP4056 - and +

Robots Contest

Participated in the
Robots Contest