Need help with robotic arm code for mBot Ranger


I have a M-Bot Ranger with the robotic arm attached and have also been able to control it through the makers platform in the M-Block app, however I want to code it so the motor only moves a certain amount (so it doesn’t start to hit itself). I am not very good with the M-Block coding system so this might be quite easy. Thanks!!


Hi @techy_apple, welcome to the forum! Could you please provide a description or photo of your setup so that I know what ports your motors are on and can give you a sample code? Thanks!


Thanks! Plugged into Port 1 is the motor that controls the arm and Port 2 is the claw.

Port 1 = Arm
Clockwise is up
Counterclock is down

Port 2 = Gripper
Clockwise is close
Counterclock is open


@techy_apple Is the gripper claw a servo motor using a servo driver, or just a normal mBot Ranger motor (the blue encoder motor)? How about the other motor? And what movements or functions would you like to accomplish? A photo would be useful if you can provide one. Thanks!


Attached is a photo of the wiring and also the motors. My goal is to make the arm pick something up, however I don’t know of a way to make the arm only move a certain amount. When I try to make the arm move (it does work) it reaches all the way to the top and then the motors tries to keep moving which results in the arm hitting the robot.


@techy_apple You could run the motor for a short time (e.g., 0.5 sec) and then stop it. May I know what extensions and code you are currently using?

Here’s an example, but without further info it is hard for me to tell what the code would be (what does your code look like right now, what extensions and device are you using, etc.):

Or you could do something like this for more precise movement (if you are using encoder motors):

I hope this helps! Good luck!


@Best_codes Using your codes idea of stoping it with 0%, I built it into my own code to create a code that can move across a room, pick up an object, and then come back to where it started. Thanks for all the help!!!


Awesome! Could you send me a screenshot of the code or post it here on the forum so that other people can use it when they need help? Thanks! @techy_apple


@Best_codes I modified my current code to use the ultrasonic senor to dectet when an object is coming close, then stop and lower the arm, open the craw and then close it again (The other little bits of code are just to re-raise and re-open the arm) When I run the code, it starts moving forward, but doesn’t seem to detect the object and lower and open the arm. Any ideas why?


@techy_apple Yes, there is an error in your code. The Set Distance to ${value, port} block should be moved inside the forever block.


This way, it will update constantly instead of only when the flag is clicked. Note that the Flag block only works in live mode, which can be laggy. If you have issues with the bot responding slowly, upload the code to it instead. Corrected code would look like this:


@everyone I made the code public. If you want, you can download it here:


@Best_codes I just found a new problem with the updated code. When the senor detects an object causing the arm to go down and open, the arm comes close enough to the senor where it think that there is nothing in its path and proceds to move forward (knocking down the object). I have tried to relocate the senor but where it is mounted is the only spot that would work within the cords distance. Any ideas on how to change the code to stop once moving forward once the arm lowers? (I also want to eventually make it so when the robot picks the object up it moves backward and drops it where the robot came from) Also thanks so much for the help so far as it has taught me lots and also improved my understanding of the robot!!


@techy_apple One moment…



No problem! I’m glad you’re learning!

Yes, here is a simple example of how we could do that:

That might be a bit complicated, but it’s possible! We’ll get there eventually.

P.S. I haven’t updated my code download link yet, so don’t use it.


@Best_codes Works very well!!! This is my new code which hopefully will pick it and lift it up a little bit too. There are most likely errors so please let me know how to correct them. The move backward after the stop moving is so the arm doesn’t hit the object since the sensor cant detect the object from that far away. The other part I added on is to actually pick up the object and then move backward. Eventually we can add the part that lowers it and sets it back on the ground. Thanks!!!


Awesome! Please let me know if there are any errors @techy_apple. I’ll do my best to troubleshoot. As of right now, I’m a bit busy, so I can’t test the code, but don’t hesitate to ask me anything else! Good luck on your project!


@Best_codes Thats okay, just test it whenever you get a chance. I will keep working on it and updating you along the way (And probably asking troubleshooting questions) Thanks!!


@techy_apple No problem! I’m glad I’ve been helpful. Keep learning!


@Best_codes Bellow is my next variation of the code. I added a part where once it picks the object up and back up, it will lower the object and place it back onto the ground. The only problem with the code/robot is that it doesn’t work perfectly every time. Sometimes the arm lowers too far and other too short. Other times it drives at a slightly angle and misses the object entirely. I haven’t found a consistent way for the program to work but it works 50% of the time. If you have any ideas please let me know. Thanks for all the help so far!


@techy_apple Your code looks awesome! Unfortunately, as your robotic arm using normal motors instead of encoder motors, there is no reliable way to tell how far the motor has spun. Thus, some imprecisities are in order.

One ting that can help it be more precise is to slow down the motors. (For example, 50% for 1 sec → 25% for 2 sec). Sadly, this may not always work. The robot driving at an angle is typical, as the pressure on mBot Rangers tracks can vary and cause alignment issues. I don’t really have many ideas. :confused: @CommandeR might have a couple, so I just mentioned him. :slight_smile:

Good luck on your robot, and keep creating, learning, and exploring! You can always ask my AI about things here:
But be aware that it is an AI, so it lies a lot and isn’t really very smart. It can give assistance on basic structural or typical knowledge.