Notifications
Clear all

Assistance on UNO and IBT2 operation on motor 12vdc

2 Posts
2 Users
0 Reactions
818 Views
(@clauded)
Member
Joined: 9 months ago
Posts: 2
Topic starter  

Good day I have a motor 12vdc with hall effect sensors 
I have a Uno and IBT2 with a N/O push button.
My operations what I want is. 
Push button, motor goes forward till max Hall Effect kicks in and stop at max open
Push button, motor goes reverse till max Hall Effect kicks in and stop at max close, but 
when motor still operates forward/reverse and the button is pushed the motor stops and when pushed again the motor goes in the opposite direction.  
Can you please confirm I'm on the correct track.

My wire diagram is as follows 

12V Power Supply
  │
  ├────(+)─── IBT-2 (VCC)
  ├────(+)─── Motor (+)
  │
  └────(+)─── Arduino Vin (if using same supply)

Ground (GND)
  │
  ├────(-)─── IBT-2 (GND)
  ├────(-)─── Motor (-)
  ├────(-)─── Hall SSR-
  └────(GND)─ Arduino GND

Arduino Connections:
D2  ────┬─── Push Button ──── GND
        └─── 10kΩ Resistor ──── 5V (internal pull-up alternative)

D5  ──── IBT-2 B-EN (PWM Speed Control)
D6  ──── IBT-2 B-PWM (PWM Speed Control)
D7  ──── IBT-2 R_EN (Enable)
D8  ──── IBT-2 R_PWM (Direction)

A0  ──── Hall SSR Out
5V  ──── Hall SSR+

and my code is as follow 

// Simplified DC Motor Control with Single Button Toggle
#define BUTTON_PIN 2
#define EN_PWM 5
#define PWM_PIN 6
#define EN_DIR 7
#define DIR_PIN 8
#define HALL_SENSOR A0

// Motor states
enum {STOPPED, OPENING, CLOSING} motorState = STOPPED;
const int FULL_SPEED = 255; // PWM value (0-255)

void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(EN_PWM, OUTPUT);
pinMode(PWM_PIN, OUTPUT);
pinMode(EN_DIR, OUTPUT);
pinMode(DIR_PIN, OUTPUT);
pinMode(HALL_SENSOR, INPUT);

stopMotor(); // Initialize motor as stopped
}

void loop() {
static bool lastButton = HIGH;
bool currentButton = digitalRead(BUTTON_PIN);

// Detect button press (falling edge)
if (lastButton == HIGH && currentButton == LOW) {
delay(50); // Simple debounce
if (digitalRead(BUTTON_PIN) == LOW) { // Still pressed
handleButtonAction();
}
}
lastButton = currentButton;

checkLimits(); // Check hall sensor limits
}

void handleButtonAction() {
switch(motorState) {
case STOPPED:
startMotor(OPENING);
break;
case OPENING:
case CLOSING:
stopMotor();
break;
}
}

void startMotor(int direction) {
motorState = direction;

// Set direction (HIGH for one direction, LOW for other)
digitalWrite(DIR_PIN, (direction == OPENING) ? HIGH : LOW);

// Enable motor
digitalWrite(EN_DIR, HIGH);
analogWrite(EN_PWM, FULL_SPEED);
analogWrite(PWM_PIN, FULL_SPEED);
}

void stopMotor() {
motorState = STOPPED;
digitalWrite(EN_DIR, LOW);
digitalWrite(EN_PWM, LOW);
}

void checkLimits() {
if (motorState != STOPPED && digitalRead(HALL_SENSOR) == LOW) {
stopMotor();
// Toggle next direction automatically
motorState = (motorState == OPENING) ? CLOSING : OPENING;
}
}


   
Quote
(@davee)
Member
Joined: 5 years ago
Posts: 2038
 

Hi @clauded,

  Sorry, I do not have an IBT-2 controller, and I am not clear as to which Hall effect sensor you are using, or how it is being used. Plus, it is always difficult to review code, beyond pointing out any obvious mistakes. (I haven't spotted any bugs, but bugs are very good at hiding, and I am bad at finding them. Testing them on real hardware is much more effective and efficient.)

-------------

So I think you have made a good start, and hope you will not mind if I suggest an alternate way forward, which is basically what I would do, if I was building your project.

When starting with a microcontroller project, that reads several sensors (e.g. switches, Hall-Effect sensors) and controls several actuators (e.g. motor controllers), I would build a number of very small sketches. (Obviously, you can use your existing code as a source for copy and pasting, to minimise the amount of typing for each sketch.)

Each sketch would test just one basic function, such as reading a switch or a sensor, or driving a motor. Typically, I would make the small block of code that does the function into a standalone function ... e.g. read the state of the switch and return open or closed. The value of the switch read could then be displayed on the Serial Monitor screen, so that you can press and release the button, and ensure the readout follows the switch state.

Similarly, I would build a sketch for each actuator, and check it works. e.g. switch on the motor for 2 seconds. If the motor has a speed control, then perhaps you would make the sketch show five different speeds, each for 2 seconds.

When you carefully tested all the individual parts, you then know that the wiring is correct, and all the parts are functional, which is a huge step forward.

You are then in a great position to combine all the parts, especially if you have built some good functions. Of course, there is no requirement to combine them in a single jump. Consider, combining 2 or 3 functions, and test it, before adding the next function, and so on.

--------

If you get stuck at any stage, please feel free to provide an update of what you have done, describing what worked as expected, and what didn't.

Good luck and best wishes, Dave



   
ReplyQuote