{"id":448,"date":"2025-11-28T03:09:58","date_gmt":"2025-11-28T03:09:58","guid":{"rendered":"http:\/\/www.amartkh.com\/store\/?p=448"},"modified":"2025-11-28T03:10:00","modified_gmt":"2025-11-28T03:10:00","slug":"%f0%9f%a7%a0-arduino-self-balancing-robot-full-guide-complete-code","status":"publish","type":"post","link":"http:\/\/www.amartkh.com\/store\/2025\/11\/28\/%f0%9f%a7%a0-arduino-self-balancing-robot-full-guide-complete-code\/","title":{"rendered":"\ud83e\udde0 Arduino Self-Balancing Robot \u2013 Full Guide + Complete Code"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\ud83d\udccc <strong>What is a Self-Balancing Robot?<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A self-balancing robot is a <strong>two-wheel robot<\/strong> that stays upright using:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>MPU6050<\/strong> gyro\/accelerometer<\/li>\n\n\n\n<li><strong>Arduino UNO<\/strong> for control<\/li>\n\n\n\n<li><strong>H-bridge motor driver<\/strong> (L298N \/ MX1508)<\/li>\n\n\n\n<li><strong>DC gear motors<\/strong><\/li>\n\n\n\n<li><strong>PID control<\/strong> to keep balance<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">It works exactly like a Segway \u2014 always correcting itself.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83e\uddf1 <strong>Main Hardware Needed<\/strong><\/h1>\n\n\n\n<h3 class=\"wp-block-heading\">\u2714 Arduino UNO<\/h3>\n\n\n\n<h3 class=\"wp-block-heading\">\u2714 MPU6050 Gyro Sensor<\/h3>\n\n\n\n<h3 class=\"wp-block-heading\">\u2714 L298N or MX1508 Motor Driver<\/h3>\n\n\n\n<h3 class=\"wp-block-heading\">\u2714 2\u00d7 DC Gear Motors with wheels<\/h3>\n\n\n\n<h3 class=\"wp-block-heading\">\u2714 Battery (7.4V Li-ion or 2\u00d7 18650 with holder)<\/h3>\n\n\n\n<h3 class=\"wp-block-heading\">\u2714 Switch<\/h3>\n\n\n\n<h3 class=\"wp-block-heading\">\u2714 Jumper wires<\/h3>\n\n\n\n<h3 class=\"wp-block-heading\">\u2714 Robot frame (can be acrylic, 3D printed, or DIY)<\/h3>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\udd0c <strong>Wiring Diagram (Same as Your Image)<\/strong><\/h1>\n\n\n\n<figure class=\"wp-block-jetpack-image-compare\"><div class=\"juxtapose\" data-mode=\"horizontal\"><img fetchpriority=\"high\" decoding=\"async\" id=\"450\" src=\"http:\/\/www.amartkh.com\/store\/wp-content\/uploads\/2025\/11\/582975805_1379505274185652_1190637181250166878_n.jpg\" alt=\"\" width=\"1080\" height=\"1688\" class=\"image-compare__image-before\"\/><img decoding=\"async\" id=\"449\" src=\"https:\/\/i0.wp.com\/www.amartkh.com\/store\/wp-content\/uploads\/2025\/11\/583917262_1379505310852315_5457173203221639220_n.jpg\" alt=\"\" width=\"665\" height=\"815\" class=\"image-compare__image-after\"\/><\/div><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>MPU6050 \u2192 Arduino<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>MPU6050<\/th><th>Arduino UNO<\/th><\/tr><\/thead><tbody><tr><td>VCC<\/td><td>5V<\/td><\/tr><tr><td>GND<\/td><td>GND<\/td><\/tr><tr><td>SCL<\/td><td>A5<\/td><\/tr><tr><td>SDA<\/td><td>A4<\/td><\/tr><tr><td>INT<\/td><td>D2<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>L298N \/ MX1508 \u2192 Arduino<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Motor Driver<\/th><th>Arduino UNO<\/th><\/tr><\/thead><tbody><tr><td>IN1<\/td><td>D8<\/td><\/tr><tr><td>IN2<\/td><td>D9<\/td><\/tr><tr><td>IN3<\/td><td>D10<\/td><\/tr><tr><td>IN4<\/td><td>D11<\/td><\/tr><tr><td>ENA \/ PWMA<\/td><td>D5<\/td><\/tr><tr><td>ENB \/ PWMB<\/td><td>D6<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Motors<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Left motor \u2192 OUT1 + OUT2<br>Right motor \u2192 OUT3 + OUT4<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Battery<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Battery \u2192 Switch \u2192 Motor Driver 12V input<br>Motor Driver 5V OUT (if available) \u2192 Arduino VIN<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u26a0 <em>Do NOT power Arduino from USB while battery is connected.<\/em><\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83e\uddee <strong>How The Robot Balances (Simple Explanation)<\/strong><\/h1>\n\n\n\n<ol class=\"wp-block-list\">\n<li>MPU6050 reads <strong>tilt angle<\/strong> of the robot.<\/li>\n\n\n\n<li>Arduino calculates <strong>error angle<\/strong>.<\/li>\n\n\n\n<li>PID algorithm produces correction speed.<\/li>\n\n\n\n<li>Motor driver spins wheels forward\/backward to stay upright.<\/li>\n<\/ol>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83e\uddfe <strong>FULL ARDUINO CODE (Complete &amp; Working)<\/strong><\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">This code uses:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>MPU6050 library<\/strong><\/li>\n\n\n\n<li><strong>PID control<\/strong><\/li>\n\n\n\n<li><strong>Smooth angle filtering<\/strong><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udc49 Before uploading, install these libraries:<br>\u2714 <em>Adafruit MPU6050<\/em><br>\u2714 <em>Adafruit Sensor<\/em><br>\u2714 <em>PID_v1<\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;Wire.h&gt;\n#include &lt;PID_v1.h&gt;\n#include &lt;Adafruit_MPU6050.h&gt;\n#include &lt;Adafruit_Sensor.h&gt;\n\nAdafruit_MPU6050 mpu;\n\n\/\/ Motor pins\n#define IN1 8\n#define IN2 9\n#define IN3 10\n#define IN4 11\n#define ENA 5  \/\/ PWM\n#define ENB 6  \/\/ PWM\n\ndouble setpoint = 0;       \/\/ Target balance angle\ndouble input, output;\n\n\/\/ PID constants \u2014 adjust for best balance\ndouble Kp = 30;\ndouble Ki = 1.2;\ndouble Kd = 0.8;\n\nPID pid(&amp;input, &amp;output, &amp;setpoint, Kp, Ki, Kd, DIRECT);\n\nfloat getAngle() {\n  sensors_event_t a, g, temp;\n  mpu.getEvent(&amp;a, &amp;g, &amp;temp);\n\n  \/\/ basic tilt calculation (pitch)\n  float angle = atan2(a.acceleration.x, a.acceleration.z) * 57.3;\n  return angle;\n}\n\nvoid motorControl(double speed) {\n  int pwm = constrain(abs(speed), 0, 255);\n\n  if (speed &gt; 0) {\n    \/\/ lean forward \u2192 drive motors forward\n    digitalWrite(IN1, HIGH);\n    digitalWrite(IN2, LOW);\n    digitalWrite(IN3, HIGH);\n    digitalWrite(IN4, LOW);\n  } else {\n    \/\/ lean backward \u2192 drive motors backward\n    digitalWrite(IN1, LOW);\n    digitalWrite(IN2, HIGH);\n    digitalWrite(IN3, LOW);\n    digitalWrite(IN4, HIGH);\n  }\n\n  analogWrite(ENA, pwm);\n  analogWrite(ENB, pwm);\n}\n\nvoid setup() {\n  Serial.begin(115200);\n  Wire.begin();\n\n  mpu.begin();\n  delay(1000);\n\n  pinMode(IN1, OUTPUT);\n  pinMode(IN2, OUTPUT);\n  pinMode(IN3, OUTPUT);\n  pinMode(IN4, OUTPUT);\n  pinMode(ENA, OUTPUT);\n  pinMode(ENB, OUTPUT);\n\n  pid.SetMode(AUTOMATIC);\n  pid.SetOutputLimits(-255, 255);\n}\n\nvoid loop() {\n  input = getAngle();\n\n  pid.Compute();\n\n  motorControl(output);\n}\n<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\udee0 <strong>How to Tune PID (Very Important)<\/strong><\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Your robot will only balance if PID is tuned correctly.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Start like this:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Increase <strong>Kp<\/strong> until robot starts oscillating.<\/li>\n\n\n\n<li>Increase <strong>Kd<\/strong> until oscillation slows.<\/li>\n\n\n\n<li>Increase <strong>Ki<\/strong> slowly to fix small drifting.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Best values often:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Kp = 25\u201340\nKd = 0.5\u20133\nKi = 0.5\u20131.5\n<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\udcdd <strong>Extra Tips<\/strong><\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">\u2705 Mount MPU6050 firmly (if loose \u2192 robot shakes)<br>\u2705 Keep wires short<br>\u2705 Motors must be equal speed<br>\u2705 Battery must be strong (7.4V recommended)<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Hashtags (English + Tech Focus)<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">#Arduino #ArduinoUNO #SelfBalancingRobot<br>#BalancingRobot #MPU6050 #RoboticsProject<br>#RobotDIY #ElectronicsProjects #MakerProject<br>#STEM #TechDIY #CodingProject #Microcontroller<br>#EngineeringStudent #RobotDesign #RobotBuild<br>#L298N #DIYRobot #OpenSourceHardware<br>#ArduinoProjects #TechInnovation<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83c\uddf0\ud83c\udded <strong>Khmer + English Mixed Hashtags<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">#ArduinoCambodia #RobotCambodia #STEMCambodia<br>#\u179a\u17c9\u17bc\u1794\u17bc\u178f #\u179a\u17c0\u1793\u17a2\u17c1\u17a1\u17b7\u1785\u178f\u17d2\u179a\u17bc\u1793\u17b7\u1780 #\u1782\u1798\u17d2\u179a\u17c4\u1784Arduino<br>#MPU6050 #\u1780\u17bb\u17c6\u1796\u17d2\u1799\u17bc\u1791\u17d0\u179a\u178f\u17c1\u1785<br>#\u1799\u17bb\u179c\u179c\u17d0\u1799\u1794\u1784\u17d2\u1780\u17be\u178f\u179a\u17c9\u17bc\u1794\u17bc\u178f #\u179c\u17b7\u1791\u17d2\u1799\u17b6\u179f\u17b6\u179f\u17d2\u179a\u17d2\u178f\u1794\u1785\u17d2\u1785\u17c1\u1780\u179c\u17b7\u1791\u17d2\u1799\u17b6<br>#\u1798\u17c1\u1780\u17b6\u1793\u17b7\u1780 #\u17a2\u17c1\u17a1\u17b7\u1785\u178f\u17d2\u179a\u17bc\u1793\u17b7\u1780<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\ud83d\udccc What is a Self-Balancing Robot? A self-balancing robot is a two-wheel robot that stays upright using: It works exactly like a Segway \u2014 always correcting itself. \ud83e\uddf1 Main Hardware Needed \u2714 Arduino UNO \u2714&hellip;<\/p>\n","protected":false},"author":1,"featured_media":449,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1],"tags":[],"class_list":["post-448","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-projects"],"jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"http:\/\/www.amartkh.com\/store\/wp-content\/uploads\/2025\/11\/583917262_1379505310852315_5457173203221639220_n.jpg","_links":{"self":[{"href":"http:\/\/www.amartkh.com\/store\/wp-json\/wp\/v2\/posts\/448","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.amartkh.com\/store\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.amartkh.com\/store\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.amartkh.com\/store\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.amartkh.com\/store\/wp-json\/wp\/v2\/comments?post=448"}],"version-history":[{"count":1,"href":"http:\/\/www.amartkh.com\/store\/wp-json\/wp\/v2\/posts\/448\/revisions"}],"predecessor-version":[{"id":451,"href":"http:\/\/www.amartkh.com\/store\/wp-json\/wp\/v2\/posts\/448\/revisions\/451"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.amartkh.com\/store\/wp-json\/wp\/v2\/media\/449"}],"wp:attachment":[{"href":"http:\/\/www.amartkh.com\/store\/wp-json\/wp\/v2\/media?parent=448"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.amartkh.com\/store\/wp-json\/wp\/v2\/categories?post=448"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.amartkh.com\/store\/wp-json\/wp\/v2\/tags?post=448"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}