Insightful Ink Walk
Friday, September 5, 2025
বাংলা
  • Home
  • Discover South Korea
    • All
    • Culture of Korea
    • Hiking in Korea
    • Korean Language
    • Study in South Korea

    Lost in the Autumn Hues of Gatbawi Mountain

    Witness South Korea’s Breathtaking Flower Spectacle: A Year-Round Guide

    Witness South Korea’s Breathtaking Flower Spectacle: A Year-Round Guide

    Hyundai’s Global Takeover: How a Korean Brand Became a Household Name in Cars

    Hyundai’s Global Takeover: How a Korean Brand Became a Household Name in Cars

    Explore Busan’s Architectural Identity: A Recap of the 2021 World Architecture Festival

    Explore Busan’s Architectural Identity: A Recap of the 2021 World Architecture Festival

    Millim cafe

    Unleash Your Inner Explorer at MILLIM Cafe: Award-Winning Design Meets Reptilian Charm in Daegu

    Beauty of Ulsan

    Discover the Beauty of Ulsan: A Seaside Paradise Awaits

    The South Korean Education System: A Model for the Knowledge-Based Society?

    The South Korean Education System: A Model for the Knowledge-Based Society?

  • Engineering
    • All
    • 3D Design & Simulation
    • Arduino Project
    • Control Engineering
    • DIY Electronics & Robotics
    • Micro-Controller Engineering
    Basic of Control-session 12

    Basic of Control Theory: Session 12 – Cutting the Cord – Wireless Control and the Road Ahead

    Basic of Control-session 11

    Basic of Control Theory: Session 11 – The Grand Finale – Orchestrating the Four-Wheel Symphony

    Basic of Control-session 10

    Basic of Control Theory: Session 10 – The Art of Tuning – Calibrating Your PID Controller

    Basic of Control-session 9

    Basic of Control Theory: Session 9 – Closing the Loop – Coding Your First PID Controller

    Basic of Control-session 8

    Basic of Control Theory: Session 8 – From Ticks to Speed – Calculating RPM

    Basic of Control-session 7

    Basic of Control Theory: Session 7 – Your First Robot Program

    Basic of Control-session 6

    Basic of Control Theory: Session 6 – From Parts to Robot – The Assembly Guide

    Basic of Control-session 5

    Basic of Control Theory: Session 5 – Assembling Your Robot’s Body

    Basic of Control-session 4

    Basic of Control Theory: Session 4 – The Robot’s Rulebook – An Introduction to Kinematics

  • Perspective of life
    • All
    • Global Topic
    • Islam
    • Perspective Brainstorming
    • Political Perspective
    many faces of Islam

    Many Faces of Islam: Unity in Diversity

    War that reshaped Middle East

    The War that Reshaped the Middle East Forever

    Trusting in Allah: Overcoming the Social Pressures of Having a Male Child

    Trusting in Allah: Overcoming the Social Pressures of Having a Male Child

    Social Media: A Journey from Word-of-Mouth to Global Networks

    Social Media: A Journey from Word-of-Mouth to Global Networks

    Metaverse Technology: A New Dimension of the Future

    Metaverse Technology: A New Dimension of the Future

    The Dark Side of Evolution Theory: Flaws that Enabled Atrocities

    The Dark Side of Evolution Theory: Flaws that Enabled Atrocities

    The Meaning of Luck in the Eye of Islam

    The Meaning of Luck in the Eye of Islam

    Does Islam Ensure the Rights of Non-Muslims? A Look at Religious Tolerance and Coexistence

    Does Islam Ensure the Rights of Non-Muslims? A Look at Religious Tolerance and Coexistence

    Remove Racism: Understanding and Addressing a Persistent Issue

    Remove Racism: Understanding and Addressing a Persistent Issue

  • Tours & Travel
    • All
    • Cafe Blog
    • Landscape and Sea
    • Life in South Korea

    Lost in the Autumn Hues of Gatbawi Mountain

    Explore Busan’s Architectural Identity: A Recap of the 2021 World Architecture Festival

    Explore Busan’s Architectural Identity: A Recap of the 2021 World Architecture Festival

    Millim cafe

    Unleash Your Inner Explorer at MILLIM Cafe: Award-Winning Design Meets Reptilian Charm in Daegu

    Rise of Islam in Bangladesh

    Unveiling the Rise of Islam in Bangladesh: A Historical Tapestry

    Beauty of Ulsan

    Discover the Beauty of Ulsan: A Seaside Paradise Awaits

  • About Me
  • Contact
No Result
View All Result
Insightful Ink Walk
  • Home
  • Discover South Korea
    • All
    • Culture of Korea
    • Hiking in Korea
    • Korean Language
    • Study in South Korea

    Lost in the Autumn Hues of Gatbawi Mountain

    Witness South Korea’s Breathtaking Flower Spectacle: A Year-Round Guide

    Witness South Korea’s Breathtaking Flower Spectacle: A Year-Round Guide

    Hyundai’s Global Takeover: How a Korean Brand Became a Household Name in Cars

    Hyundai’s Global Takeover: How a Korean Brand Became a Household Name in Cars

    Explore Busan’s Architectural Identity: A Recap of the 2021 World Architecture Festival

    Explore Busan’s Architectural Identity: A Recap of the 2021 World Architecture Festival

    Millim cafe

    Unleash Your Inner Explorer at MILLIM Cafe: Award-Winning Design Meets Reptilian Charm in Daegu

    Beauty of Ulsan

    Discover the Beauty of Ulsan: A Seaside Paradise Awaits

    The South Korean Education System: A Model for the Knowledge-Based Society?

    The South Korean Education System: A Model for the Knowledge-Based Society?

  • Engineering
    • All
    • 3D Design & Simulation
    • Arduino Project
    • Control Engineering
    • DIY Electronics & Robotics
    • Micro-Controller Engineering
    Basic of Control-session 12

    Basic of Control Theory: Session 12 – Cutting the Cord – Wireless Control and the Road Ahead

    Basic of Control-session 11

    Basic of Control Theory: Session 11 – The Grand Finale – Orchestrating the Four-Wheel Symphony

    Basic of Control-session 10

    Basic of Control Theory: Session 10 – The Art of Tuning – Calibrating Your PID Controller

    Basic of Control-session 9

    Basic of Control Theory: Session 9 – Closing the Loop – Coding Your First PID Controller

    Basic of Control-session 8

    Basic of Control Theory: Session 8 – From Ticks to Speed – Calculating RPM

    Basic of Control-session 7

    Basic of Control Theory: Session 7 – Your First Robot Program

    Basic of Control-session 6

    Basic of Control Theory: Session 6 – From Parts to Robot – The Assembly Guide

    Basic of Control-session 5

    Basic of Control Theory: Session 5 – Assembling Your Robot’s Body

    Basic of Control-session 4

    Basic of Control Theory: Session 4 – The Robot’s Rulebook – An Introduction to Kinematics

  • Perspective of life
    • All
    • Global Topic
    • Islam
    • Perspective Brainstorming
    • Political Perspective
    many faces of Islam

    Many Faces of Islam: Unity in Diversity

    War that reshaped Middle East

    The War that Reshaped the Middle East Forever

    Trusting in Allah: Overcoming the Social Pressures of Having a Male Child

    Trusting in Allah: Overcoming the Social Pressures of Having a Male Child

    Social Media: A Journey from Word-of-Mouth to Global Networks

    Social Media: A Journey from Word-of-Mouth to Global Networks

    Metaverse Technology: A New Dimension of the Future

    Metaverse Technology: A New Dimension of the Future

    The Dark Side of Evolution Theory: Flaws that Enabled Atrocities

    The Dark Side of Evolution Theory: Flaws that Enabled Atrocities

    The Meaning of Luck in the Eye of Islam

    The Meaning of Luck in the Eye of Islam

    Does Islam Ensure the Rights of Non-Muslims? A Look at Religious Tolerance and Coexistence

    Does Islam Ensure the Rights of Non-Muslims? A Look at Religious Tolerance and Coexistence

    Remove Racism: Understanding and Addressing a Persistent Issue

    Remove Racism: Understanding and Addressing a Persistent Issue

  • Tours & Travel
    • All
    • Cafe Blog
    • Landscape and Sea
    • Life in South Korea

    Lost in the Autumn Hues of Gatbawi Mountain

    Explore Busan’s Architectural Identity: A Recap of the 2021 World Architecture Festival

    Explore Busan’s Architectural Identity: A Recap of the 2021 World Architecture Festival

    Millim cafe

    Unleash Your Inner Explorer at MILLIM Cafe: Award-Winning Design Meets Reptilian Charm in Daegu

    Rise of Islam in Bangladesh

    Unveiling the Rise of Islam in Bangladesh: A Historical Tapestry

    Beauty of Ulsan

    Discover the Beauty of Ulsan: A Seaside Paradise Awaits

  • About Me
  • Contact
No Result
View All Result
Insightful Ink Walk
No Result
View All Result
Home Engineering Control Engineering

Basic of Control Theory: Session 7 – Your First Robot Program

by ahmedneaz
August 3, 2025
in Control Engineering, DIY Electronics & Robotics
0
Basic of Control-session 7

Basic of Control-session 6

126
SHARES
1.4k
VIEWS

Welcome back to the workshop! Over the last six sessions, we’ve journeyed from abstract theory to a fully assembled robot sitting on your desk. It has a brain (the Arduino Mega), a skeleton (the chassis), muscles (the motors), and a nervous system (the wiring). But right now, it’s a dormant creation, waiting for a spark of life.

Today, we provide that spark. We’re diving into the Arduino Integrated Development Environment (IDE), the digital workshop where we’ll write the very first lines of code to command our robot. We’ll start by making sure the motors spin correctly, and then we’ll write the code to listen to our encoders, giving our robot its first sense of self.

This is the moment of truth. Let’s bring our machine to life.

Part 1: Setting Up Your Digital Workshop

The Arduino IDE is the software we’ll use to write code and upload it to our robot’s brain. If you haven’t already, download and install it from the official Arduino website.

Once installed, open the IDE and follow these two simple setup steps:

  1. Select Your Board: Go to Tools > Board > Arduino AVR Boards and select Arduino Mega or Mega 2560.
  2. Select Your Port: Connect your robot to your computer with a USB cable. Go to Tools > Port and select the port that has your Arduino Mega listed next to it (e.g., COM3 on Windows or /dev/cu.usbmodemXXXX on Mac).

Your workshop is now ready.

Part 2: The “Hello, World!” of Robotics – Testing the Motors

Before we can do anything complex, we need to confirm that our wiring from Session 6 is correct and that we can command each motor to move. We’ll write a simple program to spin one motor forward, then backward.

In the Arduino world, we use three key functions to control our L298N motor drivers:

  • pinMode(pin, OUTPUT);: This tells the Arduino that a specific pin will be used to send signals out to a component.
  • digitalWrite(pin, HIGH/LOW);: This sends a simple on or off signal. We’ll use this for the direction pins (IN1, IN2, etc.).
  • analogWrite(pin, speed);: This sends a variable signal, known as Pulse Width Modulation (PWM), which the L298N interprets as speed. The speed value can range from 0 (stopped) to 255 (full speed).

Copy the following code into your Arduino IDE. This code is written for the Front-Left motor we wired up in the last session.

C++

// --- Pin Definitions for Front-Left Motor ---
#define ENA 2  // Speed control pin (must be a PWM pin)
#define IN1 22 // Direction pin 1
#define IN2 24 // Direction pin 2

void setup() {
  // Set all the motor control pins to outputs
  pinMode(ENA, OUTPUT);
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);

  // Initialize Serial Monitor for debugging
  Serial.begin(9600);
  Serial.println("Motor Test: Front-Left");
}

void loop() {
  // --- Spin Forward at Half Speed ---
  Serial.println("Spinning forward...");
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  analogWrite(ENA, 128); // Speed from 0 (stop) to 255 (full)
  delay(2000); // Wait for 2 seconds

  // --- Stop ---
  Serial.println("Stopping...");
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  delay(1000); // Wait for 1 second

  // --- Spin Backward at Half Speed ---
  Serial.println("Spinning backward...");
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  analogWrite(ENA, 128);
  delay(2000); // Wait for 2 seconds

  // --- Stop ---
  Serial.println("Stopping...");
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  delay(1000); // Wait for 1 second
}

Upload this code to your robot. You should see the front-left wheel spin forward for two seconds, stop, then spin backward for two seconds.

Troubleshooting & Testing All Motors:

  • If nothing happens: Double-check your wiring against the diagram in Session 6. Ensure your battery is charged and connected.
  • If the motor spins in the wrong direction: The easiest fix is physical. Simply swap the two motor wires where they connect to the L298N’s output terminals.
  • To test the other motors: Change the pin numbers at the top of the code to match the pins for the next motor (e.g., Front-Right uses ENA=4, IN1=30, IN2=32). Upload and test each wheel one by one until you’ve confirmed all four are working correctly.

Part 3: Giving Your Robot Senses – Reading the Encoders

A motor that just spins blindly is an open-loop system. To create our intelligent closed-loop controller, we need feedback. This is the job of the encoders.

An encoder sends out rapid pulses as the wheel turns. Trying to catch every single pulse in our main loop() is nearly impossible; the code would be too slow. Instead, we use a powerful feature of microcontrollers called interrupts. An interrupt forces the processor to pause whatever it’s doing, run a special piece of code (called an Interrupt Service Routine or ISR), and then resume its main task. This ensures we never miss a tick.

This code will read the encoder on the Front-Left wheel. The volatile keyword is important; it tells the compiler that this variable can change at any time (which it will, inside the interrupt).

C++

// --- Pin Definitions for Front-Left Encoder ---
#define ENCODER_A 21 // Must be an interrupt pin
#define ENCODER_B 20 // Must be an interrupt pin

// A 'volatile' variable can be changed by an interrupt
volatile long encoder_ticks = 0;

// Interrupt Service Routine (ISR)
// This function is called every time ENCODER_A changes state (rises or falls)
void readEncoder() {
  // Read the state of the other encoder pin to determine direction
  int b = digitalRead(ENCODER_B);
  if (b > 0) {
    // If B is high, we are spinning forward
    encoder_ticks++;
  } else {
    // If B is low, we are spinning backward
    encoder_ticks--;
  }
}

void setup() {
  // Initialize Serial Monitor
  Serial.begin(9600);
  Serial.println("Encoder Test: Front-Left");

  // Set encoder pins as inputs
  pinMode(ENCODER_A, INPUT);
  pinMode(ENCODER_B, INPUT);

  // Attach the interrupt to the ENCODER_A pin
  // The 'readEncoder' function will be called on every CHANGE of the pin's state
  attachInterrupt(digitalPinToInterrupt(ENCODER_A), readEncoder, CHANGE);
}

void loop() {
  // Print the current tick count to the Serial Monitor
  Serial.print("Ticks: ");
  Serial.println(encoder_ticks);
  
  delay(100); // Update 10 times per second
}

Upload this code. Now, open the Serial Monitor (Tools > Serial Monitor). When you manually spin the front-left wheel by hand, you should see the “Ticks” count go up when you spin it one way and down when you spin it the other. This confirms our robot now has a sense of its own motion!

What’s Next?

We’ve achieved two massive milestones today. We can command our motors, and we can sense their movement. We have successfully tested the two fundamental halves of a closed-loop system.

But “ticks” are not a very useful unit for control. To implement our PID controller, we need to know the wheel’s speed in a standard unit, like Revolutions Per Minute (RPM). In our next session, we will write the code to convert the raw tick count from our encoders into a real-time speed calculation. Once we know how fast our wheels are actually going, we’ll be just one step away from implementing the full PID brain.

Share50Tweet32SendShare9Send
Previous Post

Basic of Control Theory: Session 6 – From Parts to Robot – The Assembly Guide

Next Post

Basic of Control Theory: Session 8 – From Ticks to Speed – Calculating RPM

Next Post
Basic of Control-session 8

Basic of Control Theory: Session 8 – From Ticks to Speed – Calculating RPM

Basic of Control-session 9

Basic of Control Theory: Session 9 - Closing the Loop – Coding Your First PID Controller

Basic of Control-session 10

Basic of Control Theory: Session 10 – The Art of Tuning – Calibrating Your PID Controller

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Basic of Control-session 12
Control Engineering

Basic of Control Theory: Session 12 – Cutting the Cord – Wireless Control and the Road Ahead

by ahmedneaz
August 3, 2025
2

Hello and welcome, for one last time, to our series on the secret brains of everything. Over the past eleven...

Read more
Basic of Control-session 11

Basic of Control Theory: Session 11 – The Grand Finale – Orchestrating the Four-Wheel Symphony

August 3, 2025
Basic of Control-session 10

Basic of Control Theory: Session 10 – The Art of Tuning – Calibrating Your PID Controller

August 3, 2025
Basic of Control-session 9

Basic of Control Theory: Session 9 – Closing the Loop – Coding Your First PID Controller

August 3, 2025
Basic of Control-session 8

Basic of Control Theory: Session 8 – From Ticks to Speed – Calculating RPM

August 3, 2025

Recent News

Basic of Control-session 12

Basic of Control Theory: Session 12 – Cutting the Cord – Wireless Control and the Road Ahead

August 3, 2025
Basic of Control-session 11

Basic of Control Theory: Session 11 – The Grand Finale – Orchestrating the Four-Wheel Symphony

August 3, 2025

Follow Us

Site

  • About
  • Privacy & Policy
  • Cookies Consent
  • Terms & Conditions
  • Contact

Motive

The beauty of ink lies not only in its permanence on paper but in its ability to capture fleeting thoughts and emotions, leaving a tangible record of our innermost selves.

  • About
  • Privacy & Policy
  • Cookies Consent
  • Terms & Conditions
  • Contact

© 2025 The Insightful Ink Walk by AHMED NEAZ.

No Result
View All Result
  • Home
  • Engineering
  • Perspective of life
  • Discover South Korea
  • Tours & Travel
  • About Me
  • Contact

© 2025 The Insightful Ink Walk by AHMED NEAZ.

This website uses cookies. By continuing to use this website you are giving consent to cookies being used. Visit our Privacy and Cookie Policy.