Page 1 of 1

Advanced motor control

Posted: 05 Oct 2023, 06:41
by Oleg
Previously, we learned how to control the rotational speed of an electric motor, but we haven't yet mastered changing its direction of rotation. In this chapter, we will explore various methods for controlling the direction of rotation of direct current (DC) motors. Specifically, we will examine several special integrated circuits and modules designed for simpler control of both the speed and direction of DC motors.

Being able to reverse the direction of motor rotation can be quite useful. For instance, linear actuators open and close windows and doors like this: when a DC motor rotates in one direction, it opens, and when it rotates in the opposite direction, it closes. Similarly, if you are building a small wheeled robot, you probably want it to move both forward and backward.

Let's say we have a motor with two output contacts: A and B (as shown in the diagram below). When a positive potential is applied to contact A and a negative potential to B, the motor rotates in one direction. If you reverse the polarity of the connections, the motor will rotate in the opposite direction.

Therefore, if you want to control the direction of rotation of an electric motor, you need to somehow change the polarity of the current supplied to it. This goal is achieved through a special electrical circuit called an H-bridge.
Controlling the direction of rotation of the motor
image.png (58.1 KiB)
Controlling the direction of rotation of the motor Viewed 4619 times

H-bridges

Posted: 05 Oct 2023, 06:43
by Oleg
The operation principle of an H-bridge is illustrated in the diagram below. In this variant, we will first explore how switches are used to change the polarity of the current before moving on to working with transistors and integrated circuits.
H-bridge that uses switches
image.png (23.6 KiB)
H-bridge that uses switches Viewed 4618 times
So, if all four switches are open, no current flows to the motor. However, if switches S1 and S4 are closed while S2 and S3 remain open, current will flow from the positive pole of the power source through switch S1 to contact A of the motor, then through the motor itself and switch S4 to the negative pole of the power source, causing the motor to rotate in one direction.

If switches S1 and S4 are opened while S2 and S3 are closed, current will flow from the positive pole of the power source to contact B of the motor, and then through the motor and switch S2 to the negative pole of the power source, reversing the direction of rotation.

The table below shows how this scheme operates with various switch positions: 0 means the switch is open, 1 means it's closed (allowing current to pass), and x means the switch position doesn't matter.

We've discussed how to change the motor's rotation direction using the switches in the H-bridge. However, there are some additional switch combinations you should be aware of.

• Firstly, if all switches are open, no current will flow through the motor, and it won't rotate.

• Particularly important are combinations where the positive power source is directly connected to the negative source. This situation is called a short circuit and can lead to catastrophic consequences since it creates a very strong current in the circuit.

• Another situation arises when there is no short circuit, but the motor's contacts are effectively connected to each other. This results in an interesting effect called motor braking, where the motor quickly decelerates if it was previously in motion or refuses to rotate if it was at rest. If the motor was driving the wheels of, say, a toy car, this braking can prevent the car from rolling down a slope.

Table 8.1. Switch Combinations
Switch combinations
image.png (62.92 KiB)
Switch combinations Viewed 4618 times

H-bridge on the integrated microchip L293D

Posted: 05 Oct 2023, 06:45
by Oleg
Among hobbyist builders, the integrated microchip L293D, which contains an H-bridge, is quite popular (we will use it shortly in the "Experiment: Controlling the Direction and Speed of Motor Rotation" section). This microchip is excellent for working with small motors designed for a current of no more than 600 mA and a voltage of up to 36 V.

The L293D chip contains two H-bridges, as well as some additional components that allow it to automatically shut down if it begins to overheat. While it is possible to damage the L293D if used incorrectly, doing so is still quite challenging.

The key parameters of this microchip are as follows:
• Motor voltage ranges from 4.5 to 36 V.
• Continuous motor current: 600 mA.
• Peak motor current: 1.2 A.
• All outputs have diodes for protection against occasional voltage spikes produced by the motor.
• Thermal protection.
• Compatibility with 3- and 5-volt logic (Pi and Arduino).

The diagram below shows the internal structure and pins of this microchip and demonstrates how to control two DC motors using it. As you can see, the circuit contains four H-bridges instead of two full H-bridges. Each H-bridge can be considered a powerful digital output capable of delivering and withdrawing currents up to 600 mA. Therefore, working with this microchip offers significant flexibility.
Schematic diagram of internal device and pins of L293D chip
image.png (46.35 KiB)
Schematic diagram of internal device and pins of L293D chip Viewed 4618 times
The microchip has separate pins for receiving logical (control) signals and for powering the motors. This allows, for example, controlling a 6-volt motor using 3.3 V logic signals from a Raspberry Pi (as we will confirm in the "Experiment: Controlling the Direction and Speed of Motor Rotation" section).

The functions of all the pins shown in the diagram are presented below. As promised earlier, we will use this microchip in the "Experiment: Controlling the Direction and Speed of Motor Rotation" section to control both the speed and direction of DC motor rotation.

Functions of the L293D Chip Pins:
Pin functions of the L293D chip
image.png (118.68 KiB)
Pin functions of the L293D chip Viewed 4618 times

Experiment: controlling the direction and speed of motor rotation

Posted: 05 Oct 2023, 07:22
by Oleg
In this experiment, we will be using the L293D microchip, placing it on a prototype board. The assembled experiment circuit includes the prototype board with the L293D microchip, Raspberry Pi, a direct current motor, and a power source, as shown in the figure below:
Schematic diagram of the experiment to control the speed and direction of rotation of an electric motor. using a Raspberry Pi assembly
image.png (162.38 KiB)
Schematic diagram of the experiment to control the speed and direction of rotation of an electric motor. using a Raspberry Pi assembly Viewed 4618 times
Components

For this experiment with Arduino and Raspberry Pi, you will need the following components:
IC1 - L293D H-Bridge Microchip
C1 - 100nF Capacitor
C2 - 16V 100µF Capacitor
M1 - Small DC Motor with a 6V power supply
Battery compartment for 4 AA batteries (6V)
400-point solderless prototype board
Male-to-Male Jumper Wires
Male-to-Female Jumper Wires (only for Raspberry Pi)

Experiment Circuit

The circuit for this experiment is shown in the figure below. Raspberry Pi or Arduino provides a control voltage of 5V to the logic pin 16 of the L293D microchip. Power for the motor is supplied from a 6-volt battery pack to its pin 8.

Practically, only one of the H-bridges of the microchip is used here, so its pin EN2 is connected to ground to deactivate the unused part.

The pins EN1, IN1, and IN2 are connected to the digital output pins of Raspberry Pi or Arduino.
L293D H-bridge wiring diagram
image.png (82.16 KiB)
L293D H-bridge wiring diagram Viewed 4618 times
CAPACITORS

As previously mentioned, using capacitors in the experiment is not necessary if we are only experimenting with this circuit for a few hours, as we are not primarily concerned with the reliability that capacitors could provide.

The capacitor positions shown in the diagram are quite typical for an H-bridge integrated circuit. Capacitor C1 is called a decoupling capacitor. It should be placed as close as possible to the microchip, between the logic signal and ground. The capacitance of capacitor C1 should not exceed 100nF (which is very small), but it helps eliminate various electrical noise that could potentially harm the logic of the microchip.

Capacitor C2 serves as an energy reservoir, which can provide power for some time, but this energy is specifically used to power the motor and is not consumed by the switching logic. The capacitance of this capacitor is usually much higher than that of C1, typically 100µF or more.


Prototype Board Layout

Before connecting the H-bridge to Arduino or Raspberry Pi, you can experiment with the circuit in standalone mode: check both the motor and the switching logic by powering them from the same 6-volt battery pack. This option is perfect if you want to test the microchip's operation without connecting it to Arduino or Raspberry Pi. However, when it comes to using the H-bridge with Arduino or Raspberry Pi, it's better to separate the power supply. The motor should receive power from the battery pack, while the logic of the microchip should be powered by Arduino or Raspberry Pi.

The figure below demonstrates the layout of the prototype board for autonomous testing. For working with Arduino or Raspberry Pi, you will hardly need to modify this layout; you will just need to rearrange some jumpers.
Layout of breadboard for offline H-bridge testing
image.png (129.33 KiB)
Layout of breadboard for offline H-bridge testing Viewed 4618 times
When placing components on the prototype board, pay special attention to the microchip. It must be positioned correctly: a small cut on one side should be directed towards the upper part of the prototype board in row 10. To the left and above this cut, you should find pin 1.

Now you can connect the battery. Initially, the motor should not rotate.

CLOCKWISE AND COUNTERCLOCKWISE
When I mention that my motor is rotating clockwise or counterclockwise, your motor may actually rotate in the opposite direction. If your motor rotates counterclockwise when I say it should rotate clockwise, it's not a problem because it will rotate clockwise when I say "counterclockwise."

If you want your motor's rotation to match the description, you can swap the connecting wires.

Sometimes it can be difficult to determine in which direction the motor is rotating because it's just a bare metal shaft. You can simply touch it; if you gently grip the shaft with your thumb and index finger, you will understand the direction of rotation. You can also cut a short strip of colored adhesive tape and attach it to the spindle to serve as a makeshift flag.


Standalone Experiment

Regardless of the direction in which the motor should rotate - clockwise or counterclockwise, the Enable contact must be connected to +V. To make the motor rotate, for example, clockwise, connect the free end of the jumper that is already connected to IN1 to +V, and connect IN2 to the row labeled GND located on the right side of the prototype board.
Starting the engine clockwise
image.png (119.43 KiB)
Starting the engine clockwise Viewed 4618 times
Now, we are going to reverse the motor's direction. You can leave the connection to Enable untouched, but you need to change the connections to IN1 and IN2 so that IN1 is now connected to GND, and IN2 is connected to +V, as shown in the diagram.

Please note: in the diagrams, IN1 and IN2 jumpers are depicted as thicker for easier differentiation from other jumpers.

Now that we have confirmed that the H-bridge is working as expected, we can connect our circuit to Arduino.
Starting the engine counterclockwise
image.png (118.6 KiB)
Starting the engine counterclockwise Viewed 4618 times
Arduino Connection

The diagram below shows how to connect the prototype board to Arduino Uno using jumpers. Contact number 11 on Arduino, used as Enable, supports PWM, and we will use the Enable contact of the L293D microchip to control the motor's speed. IN1 and IN2 can be connected to any digital contacts on Arduino; contacts 10 and 9 were chosen simply because they are located near contact 11, and when all the wires are neatly laid out, it looks cleaner.

Arduino supplies a 5V voltage to the logic part of the L293D, but it's essential to note that we are not powering the motor from Arduino; the motor's power is still coming from the batteries.

The prototype board, connected to Arduino with "male-to-male" jumpers, is shown in the diagram and is ready for operation.
Connecting breadboard with H-bridge chip to Arduino
image.png (132.37 KiB)
Connecting breadboard with H-bridge chip to Arduino Viewed 4618 times
Схема управления двигателем с помощью Arduino в сборе
image.png (165.59 KiB)
Схема управления двигателем с помощью Arduino в сборе Viewed 4618 times
Arduino Program

In the Arduino program we used in the section "Experiment: Controlling the Speed of a Direct Current Motor" (based on PWM), we used the serial monitor interface, which allowed us to send commands to the motor to adjust its speed. In this experiment, that program has been expanded; it now sends commands to control not only the speed but also the direction of rotation.


Here is the Arduino sketch for this experiment:

Code: Select all

const int enablePin = 11;  // (1)
const int in1Pin = 10;
const int in2Pin = 9;

void setup() {                 // (2)
  pinMode(enablePin, OUTPUT);
  pinMode(in1Pin, OUTPUT);
  pinMode(in2Pin, OUTPUT);
  Serial.begin(9600);
  Serial.println("Enter s (stop) or f or r followed by Duty Cycle (0 to 255). E.g. f120");
}

void loop() {                         // (3)
  if (Serial.available()) {
    char direction = Serial.read();   // (4)
    if (direction == 's') {           // (5)
      stop();                         // (6)
      return;
    }
    int pwm = Serial.parseInt();      // (7)
    if (direction == 'f') {           // (8)
      forward(pwm);
    }
    else if (direction == 'r') {
      reverse(pwm);
    }
  }
}

void forward(int pwm)          // (9)
{
  digitalWrite(in1Pin, HIGH);
  digitalWrite(in2Pin, LOW);
  analogWrite(enablePin, pwm);    
  Serial.print("Forward ");
  Serial.println(pwm);
}

void reverse(int pwm)          // (10)
{
  digitalWrite(in1Pin, LOW);
  digitalWrite(in2Pin, HIGH);
  analogWrite(enablePin, pwm);
  Serial.print("Reverse ");
  Serial.println(pwm);
}

void stop()                    // (11)
{
  digitalWrite(in1Pin, LOW);
  digitalWrite(in2Pin, LOW);
  analogWrite(enablePin, 0);
  Serial.println("Stop");
}
This sketch, although relatively long, is well-structured with functions that make it easy to modify and reuse in your own projects. Let's go through the sketch's key points based on the comments:

1. The sketch begins by defining constants for the three control pins.

2. The setup() function sets these pins as outputs, initializes the serial communication at 9600 baud, and sends a message to the serial monitor, explaining the format of commands used to control the motor.

3. The loop() function continuously checks if there is any data available from the serial monitor.

4. If data is available, the first character is read as the direction (s for stop, f for forward, r for reverse).

5. If the direction is 's', the stop() function is called, and the return statement ensures that no further code in loop() is executed.

6. The 's' command doesn't require a pwm parameter, so we don't attempt to read it from the message.

7. If the direction is 'f' or 'r' (not 's'), the parseInt() function is used to extract the pwm parameter from the remaining message.

8. Then, one of the functions, forward() or reverse(), is called depending on the direction.

9. The forward() function sets in1Pin to HIGH and in2Pin to LOW to specify the motor's rotation direction. It uses analogWrite() to control the motor speed using enablePin and the pwm parameter. A confirmation message is sent to the serial monitor.

10. The reverse() function is similar to forward(), except in1Pin is set to LOW and in2Pin is set to HIGH, causing the motor to rotate in the opposite direction.

11. The stop() function sets all control pins to LOW and sets the motor's speed to 0 using analogWrite(). A "Stop" message is sent to the serial monitor.


Loading and Running the Program

To perform the experiment with Arduino, connect your Arduino using a USB cable and upload the sketch. Open the serial port monitor, enter a command like f100 in the top part of the window, and press the Send button - the motor should start rotating very slowly in one direction.
Motor control with serial commands
image.png (67.28 KiB)
Motor control with serial commands Viewed 4618 times
Next, try entering the command f255 - the motor will start rotating at full speed. The 's' command will stop the motor, the 'r100' command will slowly start it in the opposite direction, and the 'r255' command will accelerate the motor in the reverse direction to full speed.

HANDLE THE MOTOR WITH CAUTION

Imagine what would happen if a car were moving at full speed and then suddenly went into reverse. In the case of small motors with no massive components attached, this usually doesn't cause problems. However, when working with Arduino or Raspberry Pi powered from the same source as the motor, you may find that Raspberry Pi abruptly shuts down, and Arduino resets. This occurs because when changing directions suddenly, there is a very high current draw, causing a sharp drop in electrical power.

For large motors, a sudden change in speed or direction of a massive component with significant inertia can lead to serious issues. Not only can the increased currents potentially damage the H-bridge, but the motor's bearings can also experience mechanical stress.

This should be considered when developing control software for relatively large motors. To handle the motor gently, it's better to stop it before changing direction (create a delay during which it truly comes to a halt) and then start it again in the opposite direction. If you're using auxiliary functions like forward and reverse from the section "Experiment: Controlling Motor Direction and Speed," your code might look something like this in Arduino:

Code: Select all

forward(255);
delay(200);
reverse(255);

Other integrated circuits for H-bridge operation

Posted: 05 Oct 2023, 07:29
by Oleg
Common Integrated Motor Control Chips

In the market, there is a wide array of integrated circuits designed for motor control. In this section, we will explore some of the most prevalent options.

L298N Integrated Chip

Previously, we discussed the L293D integrated chip, which can handle a maximum current of 600 mA - a relatively modest figure. If you require something more powerful, fortunately, the L293D has a "big brother" - the L298N integrated chip (www.sparkfun.com/datasheets/Components/ ... /L298N.pdf).

The key advantages of this chip are as follows:

• The motor voltage can reach up to 50 V.
• A constant current of 2 A is supplied to the motor.
• The peak current delivered to the motor is 3 A.
• Compatibility with both 3V and 5V logic (Pi and Arduino).

The pinout diagram of this chip is shown below. Its configuration closely resembles that of the L298D, but the L298N allows for monitoring the current flowing through the motor, provided you add a couple of low-power resistors to the circuit (as explained below).
1298N integrated circuit with double H-bridge
image.png (72.31 KiB)
1298N integrated circuit with double H-bridge Viewed 4617 times
As depicted in the diagram, the integrated circuit is housed in a package attached to a heat sink, which allows for handling higher power loads. The chip's pins (see table) are arranged in two rows with staggered spacing, preventing it from fitting into a standard breadboard easily. To work with it, it's easiest to purchase an inexpensive pre-made H-bridge module that includes this chip.

Most of the L298N chip's pins directly correspond to those of the L293D chip. Just like with the L293D, you can activate the half-bridges in pairs to control the motor's power when the H-bridge is engaged, using PWM.
Pin diagram of L298N chip
image.png (80.85 KiB)
Pin diagram of L298N chip Viewed 4617 times
Pin diagram of L298N chip
image.png (55.16 KiB)
Pin diagram of L298N chip Viewed 4617 times
The ability to measure the current flowing through the motor can be useful, for instance, if you want to detect when the motor is stalled (something preventing it from rotating). When this happens, the current supplied to the motor sharply increases. This phenomenon is known as "stall current." If you don't need to measure the current through each of the two H-bridges, you can simply connect pins 1 and 15 to ground.

To measure this current, you'll need to connect low-resistance resistors between pin 1 and ground and between pin 15 and ground. These resistors should be designed to handle high currents and have low resistance. The resistance value is kept low to avoid significant interference with the motor's operation. Additionally, the power rating of these resistors should be sufficient to handle the heat they generate. In this setup, the voltage across the resistor will be proportional to the current passing through it, and you can measure this voltage using an analog input on an Arduino.
Motor current measurement
image.png (62.84 KiB)
Motor current measurement Viewed 4617 times
Suppose you're working with a 12 V motor that typically draws a current of 500 mA but experiences a 2 A current when stalled. In that case, you can lower the voltage to 0.5 V to monitor the motor's stalling. To calculate the required resistance:

R = V/I = 0.5 V / 2 A = 0.25 ohms.

Adjusting this value to the nearest standard value, you get a resistor with a resistance of 0.27 ohms or 270 mOhms (milliohms, not megohms). Thus, when the current is 2 A, the actual voltage will be:

V = 1 x R = 2 A x 0.27 ohms = 0.54 V.

To calculate the power class of the resistor, multiply the voltage across it (0.54 V) by the current (2 A) to get just above 1 watt. In such a case, it's advisable to use a 2-watt resistor to allow for some margin of error.

The CURRENT SENSE A pin of the chip can be directly connected to an Arduino's analog input (Raspberry Pi does not have analog inputs). Afterward, you can measure the voltage. Since the voltage is below the 5V limit that an Arduino's analog input can handle, an approximate analog input value for a 2 A current (0.5 V) would be around 100, maintaining relatively high accuracy. The following Arduino sketch illustrates how to program it to automatically stop the motor if the current exceeds 1.5 A, indicating motor stall:

Code: Select all

const float R = 0.27;
const int in1pin = 2;
const int in2pin = 3;
const int sensePin = A0;

void setup() {
    pinMode(in1pin, OUTPUT); 
    pinMode(in2pin, OUTPUT);
    // The motor will rotate forward
    digitalWrite(in1pin, HIGH); 
    digitalWrite(in2pin, LOW);
}

void loop() {
    int raw = analogRead(sensePin);
    float v = raw / 204.6; // 204.6 - 1024 / 5V 
    float i = v / R;
    if (i > 1.5) {
        // Stop the motor
        digitalWrite(in1pin, LOW);
        digitalWrite(in2pin, LOW);
    }
}

Here, the loop function takes an analog value. This value from the analog input has a maximum value of 1023 at 5V. To convert the approximate analog value to voltage, you need to divide it by 204.6 (i.e., 1023 / 5). Then, you can calculate the current using Ohm's law.

TB6612FNG Integrated Chip

The L293D and L298N integrated chips have been on the market for many years and are considered somewhat outdated today, as they use bipolar transistors instead of MOSFETs. Consequently, such chips tend to heat up even at relatively low currents.

A more modern integrated chip in the same power class as the L293D is the TB6612FNG chip (www.sparkfun.com/datasheets/Robotics/TB6612FNG.pdf). It also features two H-bridges.

The maximum motor voltage when working with the TB6612FNG chip is only 15V, and the combined current from both H-bridges should be kept below 1.2A, although peak values can reach 3.2A. The chip also comes with built-in overheat protection mechanisms.

The TB6612FNG integrated chip is only available in a surface-mount package, but you can purchase ready-made modules that use this chip and easily fit onto a breadboard.

Modules with H-bridges

Posted: 05 Oct 2023, 07:31
by Oleg
Using Pre-Made H-Bridge Modules for Motor Control

You don't necessarily have to construct an H-bridge on your own; you can simply use pre-made modules that come with built-in H-bridges. These modules come in various shapes and sizes, designed to handle different motor currents. Below is a selection of modules with H-bridges for motor control:
Modules with H-bridges
image.png (127.27 KiB)
Modules with H-bridges Viewed 4617 times
On the left, you can see the most affordable module. It employs two L9110S microchips, each of which functions as a single H-bridge. Four terminal connectors are used to connect to two DC motors, and the six-pin header includes Ground (GND), VSS (low voltage at 5 V), and four pins for setting the motor's direction, just like in the L293D:

In the center, there is a module that utilizes the TB6612FNG microchip from Sparkfun. You can solder your own male header pins to the module's connection points to make it compatible with a breadboard. Alternatively, as shown here, you can use male-to-male jumpers and sockets.

On the right, there is a module containing the L298N microchip with a heat sink. This module has protective diodes on both sides of the heat sink and even a voltage regulator that allows you to supply up to 5 V to an Arduino. However, it might not deliver enough current for use with a Raspberry Pi. This module does not include resistors for measuring the current through the motor.

More powerful motor controllers are also available. The higher the allowable current, the more expensive the device tends to be.

You can find not only standalone modules with H-bridges but also full-fledged shields that plug into Arduino and boards that can be connected to Raspberry Pi. The image below shows three such boards:
Шилды с Н-мостами для Arduino и Raspberry Рі
image.png (118.96 KiB)
Шилды с Н-мостами для Arduino и Raspberry Рі Viewed 4617 times
On the left, you see a Sparkfun Arduino shield for motor control based on the L298P microchip (a surface-mount version of the 1298 microchip). This board includes a mounting area for connecting other components.

In the center, there is a powerful H-bridge shield that can handle currents up to 30 A. You'll be amazed at how easily it can manage high currents.

On the right, you can see the RasPiRobot V3 board that fits onto the header pins of the Raspberry Pi. It uses the TB6612FNG microchip to control two DC motors.

Project: Can Crusher for Soda Cans Using Arduino

Posted: 25 Jul 2024, 23:21
by robotwf
Image

LINEAR ACTUATORS ARE VERY POWERFUL!
The project describes a device for crushing metal cans, but this mechanism can just as easily crush a hand or anything else inadvertently placed under the press. Therefore, be cautious, especially when first using the press and wanting to adjust something in it.


Components

In addition to the Arduino Uno, the following components are needed to assemble this project:

6-inch linear actuator on 12V
L298 module with an H-bridge
4 male-to-male jumper wires
2 female-to-male jumper wires
Adapter with circular socket and screw clamps
Power supply (12V and at least 3A)
Small boards and a bit of plywood
Wood screws and carpentry tools

When purchasing a linear actuator, keep in mind that their cost can vary significantly depending on their operational characteristics. For the can crusher, you will need a device with approximately a 6-inch stroke. Make sure to check the maximum current rating of the linear actuator you choose and select an H-bridge module that can handle that current. The linear actuator I worked with had a maximum current rating of 3A, so I used an H-bridge based on the L298 chip.

Connection

The diagrams below show the connection scheme for this project and a close-up image of the Arduino and H-bridge module.

The L293 module has connector pins that by default activate both H-bridges. Therefore, we will only need two Arduino outputs connected to the IN1 and IN2 pins of the module.

Image
Image
It is convenient that the H-bridge module also has a voltage regulator providing a 5V output. You can connect it to the 5V pin of the Arduino and power the board from it.

Mechanical structure

As can be seen, the project consists of a small wooden structure. At one end of the structure, the linear actuator is installed using the brackets that come with it. The actuator's rod is attached to a wooden "press" that crushes the can when moved to the limit. Two side-limiting plywood planks are necessary to prevent the can from popping out of the press during crushing.
I do not provide exact measurements because your linear actuator may differ slightly in size from mine. It is best to mount the linear actuator on a wooden platform and then measure all distances. Remember to leave a small gap between the fully extended "press" and the stop - otherwise, the press may simply eject this stop.

Arduino program

he press activates after pressing the reset button, upon the reset command, Arduino automatically starts the linear actuator. Arduino sketch for this project:

Code: Select all

const int in1Pin = 10;
const int in2Pin = 9;

const long crushTime = 30000;  // (1)

void setup() {     // (2)
  pinMode(in1Pin, OUTPUT);
  pinMode(in2Pin, OUTPUT);
  
  crush();
  stop();
  delay(1000);
  reverse();
  stop();
}

void loop() {    
}

void crush()      // (3)
{
  digitalWrite(in1Pin, LOW);
  digitalWrite(in2Pin, HIGH);   
  delay(crushTime);
}

void reverse()   // (4)
{
  digitalWrite(in1Pin, HIGH);
  digitalWrite(in2Pin, LOW);
  delay(crushTime);
}

void stop()       // (5)   
{
  digitalWrite(in1Pin, LOW);
  digitalWrite(in2Pin, LOW);
}
Let's clarify some points of the sketch using the line-by-line breakdown provided in the comments:

1. Although the linear actuator automatically stops after completing its stroke (in my motor, it's 30 seconds), this period determines how long the motor should remain on before retracting the "press" back and allowing the next can to be placed.

2. The setup function controls the overall operation of the project. By setting both control pins as outputs, the program immediately initiates the process. The crushing action is performed using the crush function.