You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
MATLAB Arduino limit switch
5 views (last 30 days)
Show older comments
Hi, i would like to know how do I program the Arduino which controls a small DC motor. What I would like is for the motor to move in 1 direction and when the limit switch is hit the motor stops.
As of now, i cannot seem to get the for loop to work. I'm not sure if the read function is been sent to the loop. hope someone could help me. thanks in advance.
function rto_Callback(hObject, eventdata, handles)
global a
value=readDigitalPin(a, 'D11');
if (value==0)
writeDigitalPin(a, 'D8',1);
writeDigitalPin(a, 'D9',0);
else if (value==1)
writeDigitalPin(a, 'D8',0);
writeDigitalPin(a, 'D9',0);
% break
end
end
15 Comments
Walter Roberson
on 24 Feb 2018
This appears to be a callback. What is triggering it?
You are not looping inside this code.
What is the meaning of D11 being 0, or 1, or something else? Where is the documentation in the code?
If a particular value represents "limit switch has been reached", is that a pulse or continuous? If it is a pulse then the next time that the callback is triggered it is no longer going to be true and you would restart movement, potentially leading to a lot of start / stop behavior.
What are you observing as happening with the current code?
Raguvaran Veerappan
on 24 Feb 2018
Thank you very much for the prompt reply. What im trying to achieve is to enbale the motor to move 1 direction till it hits the limit switch. But with this could the limit swtich has no effect, the motor keeps running. Any idea how to program such that when the limit swtich is hit the loops breaks. Thanks once again.
function rto_Callback(hObject, eventdata, handles)
global a
value=readDigitalPin(a, 'D11'); % D11 is connected to my limit switch
if (value==0)
writeDigitalPin(a, 'D8',1); % connected the DC motor
writeDigitalPin(a, 'D9',0);% connected the DC motor
else if (value==1)
writeDigitalPin(a, 'D8',0);
writeDigitalPin(a, 'D9',0);
% break
end
end
Walter Roberson
on 24 Feb 2018
This appears to be a callback. What is triggering it?
You are not looping inside this code.
What is the meaning of D11 being 0, or 1, or something else? Where is the documentation in the code of the meaning of the pin values for D11, D8, and D9?
If a particular value represents "limit switch has been reached", is that a pulse or continuous? If it is a pulse then the next time that the callback is triggered it is no longer going to be true and you would restart movement, potentially leading to a lot of start / stop behavior.
Raguvaran Veerappan
on 25 Feb 2018
Hi could you suggest a method where i can read the limit switch value. where when the switch is not hit the motor moves but when the swtich is hit the motor stops. Thanks in advance
Walter Roberson
on 25 Feb 2018
function rto_Callback(hObject, eventdata, handles)
global a
persistent at_limit
if isempty(at_limit); at_limit = false; end
if at_limit; return; end
value = read_limit_switch(a);
if limit_switch_has_been_hit(value)
send_stop(a)
stopped = true;
else
send_move_forward(a);
end
function value = read_limit_switch(a)
value = readDigitalPin(a, 'D11');
function tf = limit_switch_has_been_hit(value)
tf = .... something I cannot determine from the above code and asked you twice about without getting an answer
function send_stop(a)
.... something I cannot determine from the above code and asked you twice about without getting an answer
function send_move_forward(a)
.... something I cannot determine from the above code and asked you twice about without getting an answer
With the above code, once the limit switch has been reached, the device will never move again in the same direction. How to resume movement in the same direction at a later time is something I cannot determine from the above code and asked you twice about without getting an answer.
Raguvaran Veerappan
on 26 Feb 2018
To Mr walter,
Thank you very much for the help.Im sorry for not answering the question as i wasnt sure what was meant by documentation. Its my first time using matlab.
What im trying to achieve ultimately is a program for a scanning stage, where when the user presses "Return to home" on the gui. The motor start to rotate in 1 direction till it hits the limit switch and the programs ends once its hit.
In my arduino,
D8,D9 are connected to the terminal of the DC motor
D11 is connected to the limit switch amd the other terminal is conneted to the 3.3v.
I have already created a spereate code for the first stage of the scan based on timming where the motor will run and stop amd scan. Im just stuck at the return to home part. As i cant seem to figure out how to create a loop using the digital reading from the limit switch.
Hooe this clarifes the question and once again thank you very much for assisiting me. I really appreciate it.
Best regard
Ragu
Walter Roberson
on 26 Feb 2018
If rto_Callback is invoked when the user clicks the Return To Home button, then you need to add a loop to the code.
When you write (0,0) or (1,0) once to the motor then is that a pulse to move a limited distance, or does it activate the motor continually until it is commanded to stop?
Raguvaran Veerappan
on 27 Feb 2018
It has to move continually till it hits the limit switch.
Walter Roberson
on 27 Feb 2018
That is the behavior you want, but to get there you need to explain what happens when you send 0 0 or 1 0 to the motor. Does one of those tell the motor to turn on and remain on with the other telling the motor to switch off? Or does one of them tell the motor to run at most a particular distance and then stop with the other one meaning "I know you will stop eventually but I want you to stop now."
David Fletcher
on 27 Feb 2018
Typically sending a pin high will cause the pin to go high (+5V) and stay high until it is driven low by writing a zero to the pin register. You say that the Arduino is connected directly to the motor - are you sure the Arduino can source enough current to drive the motor without damaging the Arduino. Usually you would use a transistor to act as a current buffer between the Arduino and motor
Walter Roberson
on 27 Feb 2018
David, notice that two pins are involved and that neither of them is pwm. The most common scenario for two pin not pwm seems to be something like a Q encoding where you send pulses to turn precise distances.
David Fletcher
on 27 Feb 2018
Maybe, or maybe he's using the other pin as a 0V ground rather than a proper circuit ground. Either way, if he's got a motor connected directly to the Arduino, I don't fancy its chances for any great length of time.
Raguvaran Veerappan
on 4 Mar 2018
To Mr Walter,
Thanks for the assistance i managed to get it to work.
cheers ragu
Yong Mun Pok
on 12 Apr 2019
To Ragu,
Can you share the working code. It will really help me in doing my project. Otherwise, can send the relevant info the my email munson0829@gmail.com
Georgi Rahnev
on 10 Oct 2021
I will be also thakful if you share. the_sooner@yahoo.com
Answers (0)
See Also
Categories
Find more on Arduino Hardware in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)