{"id":2943,"date":"2025-07-20T08:55:33","date_gmt":"2025-07-20T08:55:33","guid":{"rendered":"https:\/\/learnbydoing.dev\/?p=2943"},"modified":"2026-01-19T17:42:36","modified_gmt":"2026-01-19T17:42:36","slug":"how-to-control-a-dc-motor-using-arduino-and-pid-algorithm","status":"publish","type":"post","link":"https:\/\/learnbydoing.dev\/how-to-control-a-dc-motor-using-arduino-and-pid-algorithm\/","title":{"rendered":"How to control a DC Motor using Arduino and PID Algorithm"},"content":{"rendered":"<div data-elementor-type=\"wp-post\" data-elementor-id=\"2943\" class=\"elementor elementor-2943\" data-elementor-post-type=\"post\">\n\t\t\t\t<div class=\"elementor-element elementor-element-e28ab35 e-flex e-con-boxed e-con e-parent\" data-id=\"e28ab35\" data-element_type=\"container\" data-e-type=\"container\" id=\"content\" data-settings=\"{&quot;background_background&quot;:&quot;classic&quot;}\">\n\t\t\t\t\t<div class=\"e-con-inner\">\n\t\t<div class=\"elementor-element elementor-element-62ab6e3 e-con-full e-flex e-con e-child\" data-id=\"62ab6e3\" data-element_type=\"container\" data-e-type=\"container\">\n\t\t\t\t<div class=\"elementor-element elementor-element-62ab203 elementor-align-center elementor-widget elementor-widget-post-info\" data-id=\"62ab203\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"post-info.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<ul class=\"elementor-inline-items elementor-icon-list-items elementor-post-info\">\n\t\t\t\t\t\t\t\t<li class=\"elementor-icon-list-item elementor-repeater-item-2c98363 elementor-inline-item\" itemprop=\"about\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t<span class=\"elementor-icon-list-text elementor-post-info__item elementor-post-info__item--type-terms\">\n\t\t\t\t\t\t\t\t\t\t<span class=\"elementor-post-info__terms-list\">\n\t\t\t\t<span class=\"elementor-post-info__terms-list-item\">Arduino<\/span>, <span class=\"elementor-post-info__terms-list-item\">Control<\/span>, <span class=\"elementor-post-info__terms-list-item\">Electronics<\/span>, <span class=\"elementor-post-info__terms-list-item\">Tutoriales<\/span>\t\t\t\t<\/span>\n\t\t\t\t\t<\/span>\n\t\t\t\t\t\t\t\t<\/li>\n\t\t\t\t<\/ul>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t<div class=\"elementor-element elementor-element-0650e10 e-con-full e-flex e-con e-child\" data-id=\"0650e10\" data-element_type=\"container\" data-e-type=\"container\">\n\t\t\t\t<div class=\"elementor-element elementor-element-ac19582 elementor-view-default elementor-widget elementor-widget-icon\" data-id=\"ac19582\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"icon.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"elementor-icon-wrapper\">\n\t\t\t<div class=\"elementor-icon\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"75\" height=\"75\" viewbox=\"0 0 75 75\" fill=\"none\"><path d=\"M74.9999 75H13.1889V73.0002H71.5859L0.460938 1.87521L1.87515 0.460999L73.0001 71.586V13.1889H74.9999V75Z\" fill=\"white\"><\/path><\/svg>\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t<div class=\"elementor-element elementor-element-47aa245d e-flex e-con-boxed e-con e-parent\" data-id=\"47aa245d\" data-element_type=\"container\" data-e-type=\"container\" data-settings=\"{&quot;background_background&quot;:&quot;classic&quot;}\">\n\t\t\t\t\t<div class=\"e-con-inner\">\n\t\t\t\t<div class=\"elementor-element elementor-element-75ac785 elementor-widget elementor-widget-image\" data-id=\"75ac785\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"image.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<img fetchpriority=\"high\" decoding=\"async\" width=\"800\" height=\"450\" src=\"https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/07\/Progetto-senza-titolo-6-1024x576.jpg\" class=\"attachment-large size-large wp-image-2987\" alt=\"\" srcset=\"https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/07\/Progetto-senza-titolo-6-1024x576.jpg 1024w, https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/07\/Progetto-senza-titolo-6-300x169.jpg 300w, https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/07\/Progetto-senza-titolo-6-768x432.jpg 768w, https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/07\/Progetto-senza-titolo-6-1536x864.jpg 1536w, https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/07\/Progetto-senza-titolo-6-18x10.jpg 18w, https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/07\/Progetto-senza-titolo-6-1320x743.jpg 1320w, https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/07\/Progetto-senza-titolo-6-600x338.jpg 600w, https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/07\/Progetto-senza-titolo-6.jpg 1920w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/>\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-261c3d8 elementor-widget elementor-widget-text-editor\" data-id=\"261c3d8\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p data-pm-slice=\"1 1 []\">Controlling the speed and direction of a DC motor precisely is essential for many robotics and automation projects. In this tutorial, we will learn how to use a PID controller to regulate the velocity of two DC motors connected through an L298N driver, with feedback from wheel encoders.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-a236fe0 elementor-widget elementor-widget-text-editor\" data-id=\"a236fe0\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<h2 data-pm-slice=\"1 1 []\">Why use a PID controller for motor control?<\/h2><p>DC motors are naturally fast, but they lack precision when it comes to speed control. Without feedback, any change in load or friction causes speed variations. A PID (Proportional-Integral-Derivative) controller helps correct this by adjusting motor commands based on the error between desired and measured velocity. The result is a smoother, more accurate movement \u2014 essential for robotics applications like mobile robots or line followers.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-324d52d elementor-widget elementor-widget-text-editor\" data-id=\"324d52d\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<h3 data-start=\"1857\" data-end=\"1885\"><span style=\"vertical-align: inherit;\"><span style=\"vertical-align: inherit;\"><span style=\"vertical-align: inherit;\"><span style=\"vertical-align: inherit;\">\u00a0\ud83d\udcdd<\/span><\/span><\/span><\/span>Components used<\/h3><ul data-spread=\"false\"><li><p>Arduino Uno<\/p><\/li><li><p>L298N H-Bridge motor driver<\/p><\/li><li><p>Two DC motors with encoders<\/p><\/li><li><p>Power supply (e.g., 12V battery)<\/p><\/li><li><p>PID_v1 Arduino library<\/p><\/li><li><p>Serial monitor for command interface<\/p><\/li><\/ul>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-e9729e6 elementor-widget elementor-widget-image\" data-id=\"e9729e6\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"image.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" width=\"800\" height=\"408\" src=\"https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/07\/Screenshot-2025-07-21-170842-1024x522.png\" class=\"attachment-large size-large wp-image-2986\" alt=\"\" srcset=\"https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/07\/Screenshot-2025-07-21-170842-1024x522.png 1024w, https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/07\/Screenshot-2025-07-21-170842-300x153.png 300w, https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/07\/Screenshot-2025-07-21-170842-768x391.png 768w, https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/07\/Screenshot-2025-07-21-170842-18x9.png 18w, https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/07\/Screenshot-2025-07-21-170842-1320x673.png 1320w, https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/07\/Screenshot-2025-07-21-170842-600x306.png 600w, https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/07\/Screenshot-2025-07-21-170842.png 1387w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/>\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-e1ef5c9 elementor-widget elementor-widget-text-editor\" data-id=\"e1ef5c9\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<h3 style=\"font-family: Archivo, sans-serif; color: #1d1d1d;\" aria-level=\"2\">\ud83d\udda5\ufe0fSystem overview<\/h3><p>The robot uses:<\/p><ul data-spread=\"false\"><li><p>Encoders for real-time velocity feedback<\/p><\/li><li><p>L298N to drive the motors<\/p><\/li><li><p>PID algorithm for speed control<\/p><\/li><\/ul><p>The Arduino reads velocity commands from the serial interface (in rad\/s), computes the motor speed using encoders, and adjusts PWM outputs accordingly. Each motor has its own PID controller with fine-tuned parameters.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-765a5d5 elementor-widget elementor-widget-image\" data-id=\"765a5d5\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"image.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" width=\"800\" height=\"533\" src=\"https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/07\/a7cca124-448a-48dc-b1f4-c7286eb3c1ae.png\" class=\"attachment-large size-large wp-image-2946\" alt=\"\" srcset=\"https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/07\/a7cca124-448a-48dc-b1f4-c7286eb3c1ae.png 1536w, https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/07\/a7cca124-448a-48dc-b1f4-c7286eb3c1ae-300x200.png 300w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/>\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-7a888ec elementor-widget elementor-widget-text-editor\" data-id=\"7a888ec\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<h3>Arduino Code Structure<\/h3><p>We use the <code>PID_v1<\/code> library to handle PID computations. Here&#8217;s an overview of the main blocks of the sketch.<\/p><h3>1. Pin Definitions<\/h3>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-1ba2cef elementor-widget elementor-widget-code-highlight\" data-id=\"1ba2cef\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-tomorrow copy-to-clipboard\">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-cpp line-numbers\">\n\t\t\t\t<code readonly=\"true\" class=\"language-cpp\">\n\t\t\t\t\t<xmp>#define L298N_enA 9\n#define L298N_enB 11\n#define L298N_in4 8\n#define L298N_in3 7\n#define L298N_in2 13\n#define L298N_in1 12\n\n#define right_encoder_phaseA 3\n#define right_encoder_phaseB 5\n<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-16783b7 elementor-widget elementor-widget-text-editor\" data-id=\"16783b7\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p data-pm-slice=\"1 1 []\">These pins connect the motors and encoders to the Arduino.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-0887672 elementor-widget elementor-widget-text-editor\" data-id=\"0887672\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<h3 data-pm-slice=\"1 1 []\">2. PID Configuration<\/h3>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-7242351 elementor-widget elementor-widget-code-highlight\" data-id=\"7242351\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-tomorrow copy-to-clipboard\">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-cpp line-numbers\">\n\t\t\t\t<code readonly=\"true\" class=\"language-cpp\">\n\t\t\t\t\t<xmp>double right_wheel_cmd_vel = 0.0;\ndouble right_wheel_meas_vel = 0.0;\ndouble right_wheel_cmd = 0.0;\n\ndouble Kp_r = 11.5;\ndouble Ki_r = 7.5;\ndouble Kd_r = 0.1;\n\nPID rightMotor(&right_wheel_meas_vel, &right_wheel_cmd, &right_wheel_cmd_vel, Kp_r, Ki_r, Kd_r, DIRECT);<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-455a2f8 elementor-widget elementor-widget-text-editor\" data-id=\"455a2f8\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p data-pm-slice=\"1 1 []\">Each motor has its own PID loop with independent tuning. Similar setup applies to the left motor.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-b51cf90 elementor-widget elementor-widget-text-editor\" data-id=\"b51cf90\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<h3 data-pm-slice=\"1 1 []\">3. Interrupts and Encoder Feedback<\/h3>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-6503883 elementor-widget elementor-widget-code-highlight\" data-id=\"6503883\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-tomorrow copy-to-clipboard\">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-cpp line-numbers\">\n\t\t\t\t<code readonly=\"true\" class=\"language-cpp\">\n\t\t\t\t\t<xmp>attachInterrupt(digitalPinToInterrupt(right_encoder_phaseA), rightEncoderCallback, RISING);\nattachInterrupt(digitalPinToInterrupt(left_encoder_phaseA), leftEncoderCallback, RISING);<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-ea88649 elementor-widget elementor-widget-text-editor\" data-id=\"ea88649\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p data-pm-slice=\"1 1 []\">Each time the encoder triggers a pulse, we increment the counter and update the sign depending on direction.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-1665568 elementor-widget elementor-widget-text-editor\" data-id=\"1665568\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<h3 data-pm-slice=\"1 1 []\">4. Serial Command Interface<\/h3><p>The sketch interprets serial commands of the form:<\/p><h5><strong><code>rp1.25,ln0.75,<\/code><\/strong><\/h5><ul data-spread=\"false\"><li><p><code>r<\/code>\/<code>l<\/code>: right or left motor<\/p><\/li><li><p><code>p<\/code>\/<code>n<\/code>: direction (positive\/negative)<\/p><\/li><li><p>Value: speed in rad\/s<\/p><\/li><\/ul><p>This lets us test motor control via serial without a GUI.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-1b4dfa2 elementor-widget elementor-widget-text-editor\" data-id=\"1b4dfa2\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<h3 data-pm-slice=\"1 1 []\">5. Velocity Computation and PID Update<\/h3><p>Every 100ms, the velocity is calculated based on encoder counts:<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-561c362 elementor-widget elementor-widget-code-highlight\" data-id=\"561c362\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-tomorrow copy-to-clipboard\">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-cpp line-numbers\">\n\t\t\t\t<code readonly=\"true\" class=\"language-cpp\">\n\t\t\t\t\t<xmp>right_wheel_meas_vel = (10 * right_encoder_counter * (60.0\/385.0)) * 0.10472;<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-a4bcc3e elementor-widget elementor-widget-text-editor\" data-id=\"a4bcc3e\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p data-pm-slice=\"1 1 []\">Then, the PID controller computes a new PWM command:<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-3e9c482 elementor-widget elementor-widget-code-highlight\" data-id=\"3e9c482\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-tomorrow copy-to-clipboard\">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-cpp line-numbers\">\n\t\t\t\t<code readonly=\"true\" class=\"language-cpp\">\n\t\t\t\t\t<xmp>rightMotor.Compute();\nanalogWrite(L298N_enA, right_wheel_cmd);<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-ec4d021 elementor-widget elementor-widget-text-editor\" data-id=\"ec4d021\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p data-pm-slice=\"1 1 []\">If the command is 0, it cuts off PWM to avoid inertia-related drift.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-c108bba elementor-widget elementor-widget-text-editor\" data-id=\"c108bba\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<h2 data-pm-slice=\"1 2 []\">Visual Overview<\/h2><blockquote><p>(Insert image here: schematic of Arduino + L298N + encoders)<\/p><\/blockquote><blockquote><p>(Insert image here: flow diagram of the control loop \u2014 command -&gt; PID -&gt; motor -&gt; encoder -&gt; feedback)<\/p><\/blockquote><blockquote><p>(Insert image here: screenshot of serial monitor showing rp1.00,ln1.00 commands and feedback)<\/p><\/blockquote>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-46de8a1 elementor-widget elementor-widget-text-editor\" data-id=\"46de8a1\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<h2 data-pm-slice=\"1 1 []\">Final Notes<\/h2><p>This setup provides an effective way to control DC motors with precision using only Arduino and basic hardware. It lays the foundation for more advanced techniques such as odometry, trajectory tracking, and SLAM.<\/p><p>Tuning the PID values for your own motors is crucial: start with low values and increase until stable. Always monitor your motor temperature and avoid overdriving.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<\/div>","protected":false},"excerpt":{"rendered":"<p>Controlling the speed and direction of a DC motor precisely is essential for many robotics and automation projects. In this tutorial, we will learn how to use a PID controller to regulate the velocity of two DC motors connected through an L298N driver, with feedback from wheel encoders. Why use a PID controller for motor [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":2987,"comment_status":"closed","ping_status":"open","sticky":false,"template":"elementor_header_footer","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[68,225,69,43],"tags":[59,84,109,83,154,87,282,281,82,179,66,85,223,278,277,279,76,280,64],"class_list":["post-2943","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-arduino","category-control","category-electronics","category-tutorials","tag-arduino","tag-c","tag-control","tag-cpp","tag-dc","tag-guide","tag-h-bridge","tag-hbridge","tag-ide","tag-lbd","tag-learn-by-doing","tag-motor","tag-pid","tag-program","tag-programming","tag-speed","tag-step-by-step","tag-torque","tag-tutorial"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to control a DC Motor using Arduino and PID Algorithm - Learn by Doing!<\/title>\n<meta name=\"description\" content=\"Learn how to control DC motors with Arduino using PID and encoder feedback. Set up L298N, tune PID gains, and test via serial interface.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/learnbydoing.dev\/es\/how-to-control-a-dc-motor-using-arduino-and-pid-algorithm\/\" \/>\n<meta property=\"og:locale\" content=\"es_ES\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to control a DC Motor using Arduino and PID Algorithm\" \/>\n<meta property=\"og:description\" content=\"Learn by Doing!\" \/>\n<meta property=\"og:url\" content=\"https:\/\/learnbydoing.dev\/es\/how-to-control-a-dc-motor-using-arduino-and-pid-algorithm\/\" \/>\n<meta property=\"og:site_name\" content=\"Learn by Doing!\" \/>\n<meta property=\"article:published_time\" content=\"2025-07-20T08:55:33+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-01-19T17:42:36+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/07\/Progetto-senza-titolo-6.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1920\" \/>\n\t<meta property=\"og:image:height\" content=\"1080\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Escrito por\" \/>\n\t<meta name=\"twitter:data1\" content=\"\" \/>\n\t<meta name=\"twitter:label2\" content=\"Tiempo de lectura\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutos\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/learnbydoing.dev\\\/how-to-control-a-dc-motor-using-arduino-and-pid-algorithm\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/learnbydoing.dev\\\/how-to-control-a-dc-motor-using-arduino-and-pid-algorithm\\\/\"},\"author\":{\"name\":\"\",\"@id\":\"\"},\"headline\":\"How to control a DC Motor using Arduino and PID Algorithm\",\"datePublished\":\"2025-07-20T08:55:33+00:00\",\"dateModified\":\"2026-01-19T17:42:36+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/learnbydoing.dev\\\/how-to-control-a-dc-motor-using-arduino-and-pid-algorithm\\\/\"},\"wordCount\":467,\"publisher\":{\"@id\":\"https:\\\/\\\/learnbydoing.dev\\\/es\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/learnbydoing.dev\\\/how-to-control-a-dc-motor-using-arduino-and-pid-algorithm\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/learnbydoing.dev\\\/wp-content\\\/uploads\\\/2025\\\/07\\\/Progetto-senza-titolo-6.jpg\",\"keywords\":[\"Arduino\",\"c++\",\"control\",\"cpp\",\"DC\",\"Guide\",\"h-bridge\",\"hbridge\",\"ide\",\"lbd\",\"learn by doing\",\"motor\",\"PID\",\"program\",\"programming\",\"speed\",\"step by step\",\"torque\",\"tutorial\"],\"articleSection\":[\"Arduino\",\"Control\",\"Electronics\",\"Tutorials\"],\"inLanguage\":\"es\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/learnbydoing.dev\\\/how-to-control-a-dc-motor-using-arduino-and-pid-algorithm\\\/\",\"url\":\"https:\\\/\\\/learnbydoing.dev\\\/how-to-control-a-dc-motor-using-arduino-and-pid-algorithm\\\/\",\"name\":\"How to control a DC Motor using Arduino and PID Algorithm - Learn by Doing!\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/learnbydoing.dev\\\/es\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/learnbydoing.dev\\\/how-to-control-a-dc-motor-using-arduino-and-pid-algorithm\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/learnbydoing.dev\\\/how-to-control-a-dc-motor-using-arduino-and-pid-algorithm\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/learnbydoing.dev\\\/wp-content\\\/uploads\\\/2025\\\/07\\\/Progetto-senza-titolo-6.jpg\",\"datePublished\":\"2025-07-20T08:55:33+00:00\",\"dateModified\":\"2026-01-19T17:42:36+00:00\",\"description\":\"Learn how to control DC motors with Arduino using PID and encoder feedback. Set up L298N, tune PID gains, and test via serial interface.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/learnbydoing.dev\\\/how-to-control-a-dc-motor-using-arduino-and-pid-algorithm\\\/#breadcrumb\"},\"inLanguage\":\"es\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/learnbydoing.dev\\\/how-to-control-a-dc-motor-using-arduino-and-pid-algorithm\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"es\",\"@id\":\"https:\\\/\\\/learnbydoing.dev\\\/how-to-control-a-dc-motor-using-arduino-and-pid-algorithm\\\/#primaryimage\",\"url\":\"https:\\\/\\\/learnbydoing.dev\\\/wp-content\\\/uploads\\\/2025\\\/07\\\/Progetto-senza-titolo-6.jpg\",\"contentUrl\":\"https:\\\/\\\/learnbydoing.dev\\\/wp-content\\\/uploads\\\/2025\\\/07\\\/Progetto-senza-titolo-6.jpg\",\"width\":1920,\"height\":1080},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/learnbydoing.dev\\\/how-to-control-a-dc-motor-using-arduino-and-pid-algorithm\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/learnbydoing.dev\\\/es\\\/learn-by-doing-es\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to control a DC Motor using Arduino and PID Algorithm\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/learnbydoing.dev\\\/es\\\/#website\",\"url\":\"https:\\\/\\\/learnbydoing.dev\\\/es\\\/\",\"name\":\"Learn by Doing!\",\"description\":\"Learn Robotics the fun way\",\"publisher\":{\"@id\":\"https:\\\/\\\/learnbydoing.dev\\\/es\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/learnbydoing.dev\\\/es\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"es\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/learnbydoing.dev\\\/es\\\/#organization\",\"name\":\"Learn by Doing!\",\"url\":\"https:\\\/\\\/learnbydoing.dev\\\/es\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"es\",\"@id\":\"https:\\\/\\\/learnbydoing.dev\\\/es\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/learnbydoing.dev\\\/wp-content\\\/uploads\\\/2025\\\/06\\\/cropped-cropped-cropped-Progetto-senza-titolo-6-1.png\",\"contentUrl\":\"https:\\\/\\\/learnbydoing.dev\\\/wp-content\\\/uploads\\\/2025\\\/06\\\/cropped-cropped-cropped-Progetto-senza-titolo-6-1.png\",\"width\":512,\"height\":512,\"caption\":\"Learn by Doing!\"},\"image\":{\"@id\":\"https:\\\/\\\/learnbydoing.dev\\\/es\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/in\\\/antonio-brandi-512166bb\\\/\"]},{\"@type\":\"Person\",\"@id\":\"\",\"url\":\"https:\\\/\\\/learnbydoing.dev\\\/es\\\/author\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to control a DC Motor using Arduino and PID Algorithm - Learn by Doing!","description":"Learn how to control DC motors with Arduino using PID and encoder feedback. Set up L298N, tune PID gains, and test via serial interface.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/learnbydoing.dev\/es\/how-to-control-a-dc-motor-using-arduino-and-pid-algorithm\/","og_locale":"es_ES","og_type":"article","og_title":"How to control a DC Motor using Arduino and PID Algorithm","og_description":"Learn by Doing!","og_url":"https:\/\/learnbydoing.dev\/es\/how-to-control-a-dc-motor-using-arduino-and-pid-algorithm\/","og_site_name":"Learn by Doing!","article_published_time":"2025-07-20T08:55:33+00:00","article_modified_time":"2026-01-19T17:42:36+00:00","og_image":[{"width":1920,"height":1080,"url":"https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/07\/Progetto-senza-titolo-6.jpg","type":"image\/jpeg"}],"twitter_card":"summary_large_image","twitter_misc":{"Escrito por":"","Tiempo de lectura":"3 minutos"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/learnbydoing.dev\/how-to-control-a-dc-motor-using-arduino-and-pid-algorithm\/#article","isPartOf":{"@id":"https:\/\/learnbydoing.dev\/how-to-control-a-dc-motor-using-arduino-and-pid-algorithm\/"},"author":{"name":"","@id":""},"headline":"How to control a DC Motor using Arduino and PID Algorithm","datePublished":"2025-07-20T08:55:33+00:00","dateModified":"2026-01-19T17:42:36+00:00","mainEntityOfPage":{"@id":"https:\/\/learnbydoing.dev\/how-to-control-a-dc-motor-using-arduino-and-pid-algorithm\/"},"wordCount":467,"publisher":{"@id":"https:\/\/learnbydoing.dev\/es\/#organization"},"image":{"@id":"https:\/\/learnbydoing.dev\/how-to-control-a-dc-motor-using-arduino-and-pid-algorithm\/#primaryimage"},"thumbnailUrl":"https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/07\/Progetto-senza-titolo-6.jpg","keywords":["Arduino","c++","control","cpp","DC","Guide","h-bridge","hbridge","ide","lbd","learn by doing","motor","PID","program","programming","speed","step by step","torque","tutorial"],"articleSection":["Arduino","Control","Electronics","Tutorials"],"inLanguage":"es"},{"@type":"WebPage","@id":"https:\/\/learnbydoing.dev\/how-to-control-a-dc-motor-using-arduino-and-pid-algorithm\/","url":"https:\/\/learnbydoing.dev\/how-to-control-a-dc-motor-using-arduino-and-pid-algorithm\/","name":"How to control a DC Motor using Arduino and PID Algorithm - Learn by Doing!","isPartOf":{"@id":"https:\/\/learnbydoing.dev\/es\/#website"},"primaryImageOfPage":{"@id":"https:\/\/learnbydoing.dev\/how-to-control-a-dc-motor-using-arduino-and-pid-algorithm\/#primaryimage"},"image":{"@id":"https:\/\/learnbydoing.dev\/how-to-control-a-dc-motor-using-arduino-and-pid-algorithm\/#primaryimage"},"thumbnailUrl":"https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/07\/Progetto-senza-titolo-6.jpg","datePublished":"2025-07-20T08:55:33+00:00","dateModified":"2026-01-19T17:42:36+00:00","description":"Learn how to control DC motors with Arduino using PID and encoder feedback. Set up L298N, tune PID gains, and test via serial interface.","breadcrumb":{"@id":"https:\/\/learnbydoing.dev\/how-to-control-a-dc-motor-using-arduino-and-pid-algorithm\/#breadcrumb"},"inLanguage":"es","potentialAction":[{"@type":"ReadAction","target":["https:\/\/learnbydoing.dev\/how-to-control-a-dc-motor-using-arduino-and-pid-algorithm\/"]}]},{"@type":"ImageObject","inLanguage":"es","@id":"https:\/\/learnbydoing.dev\/how-to-control-a-dc-motor-using-arduino-and-pid-algorithm\/#primaryimage","url":"https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/07\/Progetto-senza-titolo-6.jpg","contentUrl":"https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/07\/Progetto-senza-titolo-6.jpg","width":1920,"height":1080},{"@type":"BreadcrumbList","@id":"https:\/\/learnbydoing.dev\/how-to-control-a-dc-motor-using-arduino-and-pid-algorithm\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/learnbydoing.dev\/es\/learn-by-doing-es\/"},{"@type":"ListItem","position":2,"name":"How to control a DC Motor using Arduino and PID Algorithm"}]},{"@type":"WebSite","@id":"https:\/\/learnbydoing.dev\/es\/#website","url":"https:\/\/learnbydoing.dev\/es\/","name":"Learn by Doing!","description":"Learn Robotics the fun way","publisher":{"@id":"https:\/\/learnbydoing.dev\/es\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/learnbydoing.dev\/es\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"es"},{"@type":"Organization","@id":"https:\/\/learnbydoing.dev\/es\/#organization","name":"Learn by Doing!","url":"https:\/\/learnbydoing.dev\/es\/","logo":{"@type":"ImageObject","inLanguage":"es","@id":"https:\/\/learnbydoing.dev\/es\/#\/schema\/logo\/image\/","url":"https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/06\/cropped-cropped-cropped-Progetto-senza-titolo-6-1.png","contentUrl":"https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/06\/cropped-cropped-cropped-Progetto-senza-titolo-6-1.png","width":512,"height":512,"caption":"Learn by Doing!"},"image":{"@id":"https:\/\/learnbydoing.dev\/es\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.linkedin.com\/in\/antonio-brandi-512166bb\/"]},{"@type":"Person","@id":"","url":"https:\/\/learnbydoing.dev\/es\/author\/"}]}},"_links":{"self":[{"href":"https:\/\/learnbydoing.dev\/es\/wp-json\/wp\/v2\/posts\/2943","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnbydoing.dev\/es\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnbydoing.dev\/es\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnbydoing.dev\/es\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/learnbydoing.dev\/es\/wp-json\/wp\/v2\/comments?post=2943"}],"version-history":[{"count":24,"href":"https:\/\/learnbydoing.dev\/es\/wp-json\/wp\/v2\/posts\/2943\/revisions"}],"predecessor-version":[{"id":5710,"href":"https:\/\/learnbydoing.dev\/es\/wp-json\/wp\/v2\/posts\/2943\/revisions\/5710"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/learnbydoing.dev\/es\/wp-json\/wp\/v2\/media\/2987"}],"wp:attachment":[{"href":"https:\/\/learnbydoing.dev\/es\/wp-json\/wp\/v2\/media?parent=2943"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnbydoing.dev\/es\/wp-json\/wp\/v2\/categories?post=2943"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnbydoing.dev\/es\/wp-json\/wp\/v2\/tags?post=2943"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}