• Skip to main content
  • Skip to header right navigation
  • Skip to site footer
  • YouTube
  • MakerWorld
  • Instagram
  • Threads
  • Bluesky
  • Mastodon
  • Facebook
makerhacks-logo

Maker Hacks

Ideas, news & tutorials for makers and hackers – Arduino/Raspberry Pi, 3D printing, robotics, laser cutting, and more

  • Home
  • About
  • 3D Printing
  • Laser Cutting
  • CNC
  • Programming
  • Free Arduino Course
  • Free Robotics Course
  • Recommendations

IR Remote Control

You are here: Home / Robotics Course / IR Remote Control

Now we bring together the Two Motors example and the IR remote/ receiver example to remote control our robot using infrared remote control.

I am using 2 for up/forward, 8 for down/backwards, 4 for left and 6 for right. 5 I am using for “stop”. You can change these easily using other button codes: the Infrared Sensor sketch prints the command code for each button, and this sketch prints any button it does not know too. It is written for version 4 of the IRremote library, which reports a short command code per button rather than the long codes older tutorials use.

Also tweak the power/speed – your input power source might not have enough current to do 255, but 125 might be too low for your motors, so see what works for your setup.

Code

/*
 Remote control our robot with an IR remote, driving two
 motors through the L298N H-Bridge breakout.

 Motor A: direction pins 4 and 5, speed (enable) pin 3
 Motor B: direction pins 7 and 8, speed (enable) pin 6
 IR receiver: signal to pin 2, plus 5V and GND.

 Written for version 4 of the IRremote library. The button
 codes are for the common 21-key "Car MP3" remote; run the
 Infrared Sensor lesson's sketch to find the codes for yours.
*/

#include <IRremote.hpp>

// Connect pin 2 on the Uno to the signal pin on the IR receiver
#define RECV_PIN 2

// these control direction for motor A
int pin1A = 4;
int pin2A = 5;

// these control direction for motor B
int pin1B = 7;
int pin2B = 8;

// set on/off and speed
int enablepinA = 3;
int enablepinB = 6;

// how much power to send (0 to 255)
int power = 255;

// set things up ...
void setup() {

  // start the serial connection
  Serial.begin(115200);

  // tell us we are starting
  Serial.println("Starting ...");

  // declare both pins for each motor
  // to be output:
  pinMode(pin1A, OUTPUT);
  pinMode(pin2A, OUTPUT);
  pinMode(pin1B, OUTPUT);
  pinMode(pin2B, OUTPUT);

  // enable pin is also output
  pinMode(enablepinA, OUTPUT);
  pinMode(enablepinB, OUTPUT);

  // Start the receiver, flashing the built-in LED
  // (pin 13) whenever a signal arrives
  IrReceiver.begin(RECV_PIN, ENABLE_LED_FEEDBACK);
}

// keep doing this forever:
void loop() {

  // If we get a burst of IR, decode it
  if (IrReceiver.decode()) {

    // Holding a button sends "repeat" codes; act on
    // the first press only
    if (!(IrReceiver.decodedIRData.flags & IRDATA_FLAGS_IS_REPEAT)) {

      // Decide action based on the button's command code
      switch (IrReceiver.decodedIRData.command) {

        // #2 = Forward
        case 0x18:
          Serial.println("REMOTE 2: forward");
          forward(power);
          break;

        // #4 = Left
        case 0x08:
          Serial.println("REMOTE 4: left");
          left(power);
          break;

        // #5 = Stop
        case 0x1C:
          Serial.println("REMOTE 5: stop");
          stop();
          break;

        // #6 = Right
        case 0x5A:
          Serial.println("REMOTE 6: right");
          right(power);
          break;

        // #8 = Backwards
        case 0x52:
          Serial.println("REMOTE 8: back");
          back(power);
          break;

        // Otherwise show what we got, so you can
        // add more buttons
        default:
          Serial.print("Unknown button: ");
          IrReceiver.printIRResultShort(&Serial);
          break;
      }
    }

    // Receive the next IR value
    IrReceiver.resume();
  }
}

// Drive both motors forward
void forward(int power)
{
  digitalWrite(pin1A, LOW);
  digitalWrite(pin2A, HIGH);
  digitalWrite(pin1B, HIGH);
  digitalWrite(pin2B, LOW);

  // set the speed
  analogWrite(enablepinA, power);
  analogWrite(enablepinB, power);
}

// Drive A forward and B backward
void right(int power)
{
  digitalWrite(pin1A, LOW);
  digitalWrite(pin2A, HIGH);
  digitalWrite(pin1B, LOW);
  digitalWrite(pin2B, HIGH);

  // set the speed
  analogWrite(enablepinA, power);
  analogWrite(enablepinB, power);
}

// Drive A backward and B forwards
void left(int power)
{
  digitalWrite(pin1A, HIGH);
  digitalWrite(pin2A, LOW);
  digitalWrite(pin1B, HIGH);
  digitalWrite(pin2B, LOW);

  // set the speed
  analogWrite(enablepinA, power);
  analogWrite(enablepinB, power);
}

// Drive both motors backwards
void back(int power)
{
  digitalWrite(pin1A, HIGH);
  digitalWrite(pin2A, LOW);
  digitalWrite(pin1B, LOW);
  digitalWrite(pin2B, HIGH);

  // set the speed
  analogWrite(enablepinA, power);
  analogWrite(enablepinB, power);
}

// Stop both motors
void stop()
{
  analogWrite(enablepinA, 0);
  analogWrite(enablepinB, 0);
}
Previous Infrared SensorCourse contentsNext Robot Chassis

Robot Course Lessons

  • Introduction
  • Choosing a Brain for your robot
  • Motors and Actuators
  • Basic Motor Controllers
  • Sensors
  • Breadboards, Batteries, Chassis, Wheels and Wires
  • Introduction
  • Basic DC Motor Movement
  • Forward and Reverse with Servos
  • Controlling DC Motors with the L293D H-Bridge Chip
  • Using Motor Drivers, Shields and Breakouts
  • Stepper Drivers
  • L298N Breakouts
  • Changing Direction with Tank Turning – L298n Breakout and Two Motors
  • Infrared Sensor
  • IR Remote Control
  • Robot Chassis
Creative Commons Attribution-ShareAlike 4.0 licence

Free to use, adapt and share. This course by Chris Garrett, Maker Hacks, is licensed under Creative Commons Attribution-ShareAlike 4.0. Workshops, schools and clubs are welcome to use it, including in your own handouts and sessions. Just credit “Chris Garrett, Maker Hacks (makerhacks.com)” with a link to the lesson, and share any adapted versions under the same licence. Embedded videos and other people’s content keep their own terms.

Improved a lesson or spotted a mistake? I would love to hear about it: get in touch.

  • 3D Printing
  • CNC
  • Electronics and Hardware
  • Laser Cutting
  • News and Reviews
  • Software and Programming

arduino budget cad cnc diode laser glowforge Hacks Ideas laser cutter linux Makes making python raspberry pi resin Reviews technology tips xTool Lasers

  • YouTube
  • MakerWorld
  • Instagram
  • Threads
  • Bluesky
  • Mastodon
  • Facebook

.

Maker Hacks Blog Copyright © 2026 · All Rights Reserved · Privacy Policy