Controlling linear actuator using IR sensor with Arduino Uno

Greetings Guys!

I am trying to build a protype miniature trash compressor using micro linear actuator. To control it, I am going to use IR sensor instead of buttons, the IR sensor will be responsible to detect if the trash is full. If the trash is full, the linear actuator will extend and retract back to its position. There’s also a 2 channel relay module that will act as a switch for extend and retract of the linear actuator and allowing the low-voltage Arduino to control the high-voltage actuator and battery.

Materials

Arduino UNO
IR Sensor
2 channel relay module
Bread Board
12V Battery

Code

// Define the pin numbers for IR sensor and relay module
const int irSensorPin = 2;   // Pin connected to the signal (OUT) of the IR sensor
const int relayExtendPin = 3; // Pin connected to IN1 of the relay module
const int relayRetractPin = 4; // Pin connected to IN2 of the relay module

void setup() {
  pinMode(irSensorPin, INPUT);
  pinMode(relayExtendPin, OUTPUT);
  pinMode(relayRetractPin, OUTPUT);
}

void loop() {
  // Read the IR sensor input
  int irSensorValue = digitalRead(irSensorPin);

  // If the IR sensor detects trash fullness (you may need to adjust the logic based on your sensor)
  if (irSensorValue == LOW) {
    // Extend the actuator to compress the trash
    digitalWrite(relayExtendPin, HIGH);
    delay(2000); // Adjust delay as needed
    digitalWrite(relayExtendPin, LOW);

    // Wait for a moment before retracting
    delay(2000); // Adjust delay as needed

    // Retract the actuator to its initial position
    digitalWrite(relayRetractPin, HIGH);
    delay(2000); // Adjust delay as needed
    digitalWrite(relayRetractPin, LOW);

    // Wait for a longer period before the next detection (prevent rapid triggering)
    delay(3000); // Adjust delay as needed
  }
}

1.) Is the materials possible to make this project work?

2.) Is the code correct?

3.) Where should I connect the wirings of the 12V battery, and electric linear actuator in 2 channel relay module? (NO1, NO2, NC1, NC2, COM1, COM2)

1 Like

Hey Shikai,

Interesting project, I’m assuming you’ve already got a linear actuator in mind?

  1. You’ll need to check it’s maximum current draw at stall before choosing the relay module to use, but none the less a 2-channel relay module should be fine to control a linear actuator.

Personally, if required I’d recommend prototyping it on a breadboard, then stepping up to some perfboard instead for a more reliable permanent setup:

Although with that being said, what would you need the breadboard for? Depending on your relay module all you need to interface with is the IR sensor, depending on what you go with that should only need access to a couple pins and may not require supplementary passive components.

  1. Is the code correct? - in my professional opinion that’s quite a loaded question :sweat_smile: What I can say is that based on the problem statement you’ve provided:

I am trying to build a prototype miniature trash compressor using micro linear actuator

This code appears to be a valid solution.

However, with that being said, there’s a couple changes I’d recommend.

For one, the #define pre-processor directive in this case would be preferable to const, as calling const int allocates memory on the stack which is accessed at runtime and consumes memory space on your board (in this case, not very much, and if your compiler is clever it’ll do this optimization for you). Even still, building habits early is a good thing.

General styling conventions in ArduScript define constants in UPPER_SNAKE_CASE as it is more readable and makes it more obvious what’s constant and what’s a variable. e.g. this:

int irSensorValue = digitalRead(irSensorPin);

versus this:

int irSensorValue = digitalRead(IR_PIN);

Also, leveraging functions here could make the code much neater and easier to work with.

Finally, since there’s no method for handling exceptions in ArduScript neatly because try-catch-finally doesn’t usually play nice with microcontrollers (not going to go into why rn). You need to be careful of your states, not unlike the ACID principle when you’re mutating data.

If you stop your program where your retract pin is high, then start it again. If it is still high when your extend pin goes high, that may be able to cause a short. Adding a safe_start() here to ensure that both pins are always low before extend is called is a pretty lightweight operation to avoid that.

OR you can use a H-Bridge depending on your projects budget and the specs for your actuator, the majority of H-Bridge breakouts are designed specifically to ensure that the shorting condition is not possible (or at least less easy)

Here’s an example applying these techniques:

#define IR_PIN 2       // IR sensor signal (OUT) pin
#define EXTEND_PIN 3   // relay module IN1 pin
#define RETRACT_PIN 4  // relay module IN2 pin
#define PERIOD_MS 2000 // delay between state changes in ms

void setup() {
  pinMode(IR_PIN, INPUT);
  pinMode(EXTEND_PIN , OUTPUT);
  pinMode(RETRACT_PIN, OUTPUT);
}

bool isSensorLow() {
  return digitalRead(IR_PIN) == LOW;
}

void extend() {
  digitalWrite(EXTEND_PIN, HIGH);
  delay(PERIOD_MS);
  digitalWrite(EXTEND_PIN, LOW);
  delay(PERIOD_MS);
}

void retract() {
  digitalWrite(RETRACT_PIN, HIGH);
  delay(PERIOD_MS);
  digitalWrite(RETRACT_PIN, LOW);
  delay(PERIOD_MS);
}

void safe_start() {
  digitalWrite(EXTEND_PIN, LOW);
  digitalWrite(RETRACT_PIN, LOW);
}

void loop() {
  safe_start();
  if (isSensorLow()) {
    extend();
    retract();
  }
}
  1. Where should I connect the wirings of the 12V battery, and electric linear actuator in 2 channel relay module? (NO1, NO2, NC1, NC2, COM1, COM2) - What actuator have you got? COM is usually a shorthand representation for a serial port ID. If you’ve got a list of parts available we can check and advise for you.