Triggering stepper motor on ROI activity

659 views
Skip to first unread message

Shrivas Chaterji

unread,
Sep 7, 2021, 1:02:54 AM9/7/21
to Bonsai Users
Hello!

Firstly, I'd like to compliment and thank the developers for creating Bonsai, it's a beautiful piece of software!

I've just started using Bonsai to set up a closed-loop reaching task, wherein a stepper motor attached to a roulette containing food pellets is to turn a specific amount when triggered by activity in a ROI.
I used Firmata to control the servo motor, but I can't figure out how to use it to control the stepper motor, since there are multiple connections between the motor and the Arduino, via the shield.

I'd appreciate any help regarding this!

Thanks,
Shrivas

brunocruz

unread,
Sep 7, 2021, 3:42:10 AM9/7/21
to Bonsai Users
Hi Shrivas!

Unfortunately, the way to control stepper motors often differs significantly from servos. The control of the former usually requires some sort of intermediary hardware like the A3967 IC and the hookup scheme might differ from stepper to stepper (4wire, 6wire or 8wire configuration for instance). The servos, on the other hand, are controlled with PMWs by varying the pulse width. 
The good news, from the bonsai side, depending on your application, and as long as you have the intermediary board, it is very feasible to activate the stepper with a vanilla firmata code. You set the configuration pins (direction, MSX and Enable) and the Step instruction using digitalWrites.
Any chance you can post the links of the hardware (stepper, driver or shield, etc..) you are using so I can take a look?

Cheers,
Bruno

Shrivas Chaterji

unread,
Sep 7, 2021, 10:38:40 AM9/7/21
to Bonsai Users
Hey Bruno, thanks for your reply!
Right, makes sense then that the Standard firmata has code for servos but not steppers.
I'll try coding in the pins and instructions in the firmata as you described, though examples for this, if possible, would be really helpful (I'm new to arduino too!)
I'm using a Nema 17 4.2 Kg-cm motor (https://robu.in/product/nema17-4-2-kgcm-stepper-motor/) with a L298N based motor driver module (https://robu.in/product/l298-based-motor-driver-module-2a/?gclid=Cj0KCQjwpdqDBhCSARIsAEUJ0hM8OigXFo-d6ZitG8OWDwH9GsUJvI6NZ1vbZpSoS6FHg9HyarZTlE0aAhoDEALw_wcB)

Thanks again!

brunocruz

unread,
Sep 7, 2021, 12:10:36 PM9/7/21
to Bonsai Users
Hey,

I don't have experience with that driver tbh. But from what I can tell you need the Stepper library to use it. I can see a couple of options if that is the controller you indeed want to use:

1. Modify firmata to include that library and send messages from bonsai with instructions (no idea how much work this would take)
2. Forget firmata and use the general Serial communication protocol wherein you send a number to be read by Arduino which tells it how much it will rotate by. I find this one a bit easier to implement since it allows you to build on top of the Arduino code you are already using and just add a few lines to parse an incoming serial message.

Alternatively, I find systems such as this one much easier to interface with. Instead of generating complicated pulse sequences, that rely on external libraries, the control is achieved by simply toggling some pins. Which can be achieved very easily in Bonsai + firmata :).

I hope it helps 
Bruno

Shrivas Chaterji

unread,
Sep 8, 2021, 12:55:30 AM9/8/21
to Bonsai Users
Hi,

Thanks so much, I'll have a look at the driver you sent, I could perhaps get it!
It might be fun trying to interface the one I have too, I'll see what I can do!
Thanks again!

Shrivas Chaterji

unread,
Sep 8, 2021, 10:37:02 AM9/8/21
to Bonsai Users
Hi,

I'm trying to use SerialStringWrite to communicate directly with the arduino, but I keep running into an "Access to com port denied" error. I read elsewhere that changing settings in Manage Ports might solve the issue, but I can't find Manage Ports in the Port Name dropdown list of SerialStringWrite even after installing the System Design package.
I'd appreciate help with why I might be getting this error, and what else I could do to get the Manage Ports option. Also, COM4, the port the arduino is connected to, shows up 4 times on the dropdown list for Port Name.

brunocruz

unread,
Sep 8, 2021, 11:01:08 AM9/8/21
to Bonsai Users
Find attached a small demo.
From Bonsai side, it sends a "1" or a "2" whenever you press "A" or "S".  Additionally, it also keeps reading serial info from Arduino.
From the Arduino side, it reads incoming bytes, tries to parse them as Int and sends them back to Bonsai.

In bonsai, you should see in SerialRead the same value you send (1 or 2)

Let me know if it works ;)
Bruno
ArduinoSerialTest.ino
BonsaiSerialComDemo.bonsai

Shrivas Chaterji

unread,
Sep 9, 2021, 2:41:36 AM9/9/21
to Bonsai Users
This worked, thank you!
I think I needed to create a new serial port to the arduino instead of directly trying to communicate over the port the arduino was connected to (though I'm still not sure why!).
It also works with the sketch for the stepper motor now, though arduino seems to receive signals multiple times for each key press, but that could just be a problem with the code.

Thanks again, this was really helpful!
Reply all
Reply to author
Forward
0 new messages