I am hoping somebody can help me I am trying to program and control a linear actuator with hall effect sensor. Firstly I really have tried to do this by myself by following a tutorial and a YouTube post from Firgelli Automation but its not working.
Here is the tutorial: https://www.firgelliauto.com/blogs/tutorials/feedback-from-a-hall-effect-sensor
Here is the You Tube video: Video
Components:
- UNO R3
- Actuator model: Zoom Industrial Z0600WH, 5 wires red and black for power, yellow wire says its the sensor wire, the green wire is the ground and the white wire 5V for sensor.
- BTS 7690 Motor controller board
I have drawn up the schematic of the hardware and attached it.
The code is meant to first home the actuator, when I activate the Uno the actuator just jogs briefly.
The two buttons are then meant to control the extension and the retraction of the actuator, both buttons for some reason extend the actuator? Also the serial output shows a value of 1.4 when its used the other outputs 0.0?
At this point I don't know if the code is wrong or I have some wiring wrong? The guy on the video does not really explain some of the connections but I think I copied them correctly.
Hope somebody can help point me in the right direction. Thanks
/*Hall effect linear actuator code with feedback, full code
*/
long pos = 0; // Actuator Position
long prevPos = 1; // Previous Position, starts !=pos
long steps = 0; // Pulses from Hall effect sensors
long prevsteps = 0; // Previous steps counted
float conNum = 0.0000286; // Convert to inches
bool dir = 0; // Direction of actuator (0= retract, 1=Extend)
int Speed = 0; // Speed Variable
bool homeFlag = 0; // Flag use to know if the actuator is home
unsigned long prevTimer = 0; // Previous Time Stamp
unsigned long lastStepTime = 0; // Time stamp of last pulse
int trigDelay = 500; // Delay between pulse in microseconds
void setup() {
pinMode(10, OUTPUT); // Configure pin 10 as an output
pinMode(11, OUTPUT); // Configure pin 11 as an output
pinMode(8, INPUT_PULLUP); // Input for Button
pinMode(9, INPUT_PULLUP); // Input for Button
/* Setting Up Interrupt*/
pinMode(2, INPUT);
attachInterrupt(digitalPinToInterrupt(2), countSteps, RISING);
Serial.begin(9600);
}
void loop() {
/*Homing the Linear Actuator*/
if(homeFlag == 0){homeActuator();}
/*Run Linear Actuator*/
if(digitalRead(8) == HIGH & digitalRead(9) == LOW){
// Retract Actuator
dir = 0;
Speed = 255;
analogWrite(10, 0);
analogWrite(11, Speed);
if(millis() - prevTimer > 100){ //Update the position Every 1/10 second
updatePosition();
prevTimer = millis();
if(pos == prevPos | pos == 0){ pos = 0;} // Corrects Position
else {prevPos = pos;}
Serial.println(convertToInches(pos));
}
}
else if(digitalRead(8) == LOW & digitalRead(9) == HIGH){
//Extend Actuator
dir = 1;
Speed = 255;
analogWrite(10, 0);
analogWrite(11, Speed);
if(millis() - prevTimer > 100){ //Update the position Every 1/10 second
updatePosition();
prevTimer = millis();
if(pos == prevPos | pos == 49000){ pos = 49000;} // Corrects Position
else {prevPos = pos;}
Serial.println(convertToInches(pos));
}
}
else{
// Stop Actuator
Speed = 0;
analogWrite(10, 0);
analogWrite(11, 0);
}
}
/*InterruptService Routine*/
void countSteps(void) {
if(micros()-lastStepTime > trigDelay){
steps++;
lastStepTime = micros();
}
}
/*Homing Function*/
void homeActuator(void){
prevTimer = millis();
while(homeFlag == 0){
Speed = 255;
analogWrite(10, 0);
analogWrite(11, Speed);
if(prevsteps == steps){
if(millis() - prevTimer > 100){
analogWrite(10, 0);
analogWrite(11, 0);
steps = 0;
Speed = 0;
homeFlag = 1;
}
}else{
prevsteps = steps;
prevTimer = millis();
}
}
}
/*Update Position Function*/
void updatePosition(void){
if(dir == 1){
pos = pos + steps;
steps = 0;
} else {
pos = pos - steps;
steps = 0;
}
}
/*Convert To Inches Function*/
float convertToInches(long pos){
return conNum*pos;
}