archipi
Posts: 10
Joined: Mon Jun 03, 2019 11:09 am

Arduino motor control from Pi 3B+

Mon Jul 15, 2019 11:02 am

Hi,

I'm working on my first (robotics) project with the Pi and would like to connect it to the Arduino Uno to control some motors from there as well. I've already set up a bluetooth connection to the Arduino with a HC-06 and can send text messages from the Pi terminal to the Arduino Serial Monitor. The way my project works is that I have an adafruit Motor HAT on my Pi and I control two stepper motors from there through a Python script. It's connected to a Wiimote so a button on the remote is linked to one motor each. I would like to extend it to connect further motors to the Wiimote and run them from the Arduino Uno (which I already have anyway). I need to be able to run two motors that are not connected to the Pi via wires because of the setup of my project.

So, to my question:

Can I control the Arduino from a Python script run on my Pi via bluetooth? I have found tutorials for the Serial Port but I'm not sure if that's what I need. Maybe I don't understand it correctly but So far I understand that you can send data and text messages that way?

If so, any tutorials that I could follow or tips? :?:

P.S. I had also considered buying a Pi Zero W to control the "wireless" motors but since I already have the Arduino I wanted to try it this way first.

User avatar
DougieLawson
Posts: 35793
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Arduino motor control from Pi 3B+

Mon Jul 15, 2019 2:44 pm

How are you getting bluetooth on your Arduino Uno. They don't have it without additional hardware. The Uno doesn't even have WiFi or any wireless on a bare board.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

archipi
Posts: 10
Joined: Mon Jun 03, 2019 11:09 am

Re: Arduino motor control from Pi 3B+

Mon Jul 15, 2019 3:02 pm

That's what I'm using the HC-06 Bluetooth module for. I have it wired up to the Arduino with the breadboard. And I don't think I will need WiFi on it

User avatar
DougieLawson
Posts: 35793
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Arduino motor control from Pi 3B+

Mon Jul 15, 2019 3:05 pm

WiFi with MQTT as the command and control system is an order of magnitude easier. Your Raspberry can be the WiFi access point for the whole set-up.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

archipi
Posts: 10
Joined: Mon Jun 03, 2019 11:09 am

Re: Arduino motor control from Pi 3B+

Mon Jul 15, 2019 3:47 pm

Could you elaborate on what that is? I've not come across it and was pretty fixed on using Bluetooth, because I'm not sure I will have internet at the location where my project setup will be

gordon77
Posts: 4111
Joined: Sun Aug 05, 2012 3:12 pm

Re: Arduino motor control from Pi 3B+

Mon Jul 15, 2019 5:47 pm

I have never tried it but this looks promising. It maybe a bit out of date as it uses a usb dongle on the pi.

http://www.uugear.com/portfolio/bluetoo ... d-arduino/

User avatar
DougieLawson
Posts: 35793
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Arduino motor control from Pi 3B+

Mon Jul 15, 2019 6:58 pm

archipi wrote:
Mon Jul 15, 2019 3:47 pm
Could you elaborate on what that is? I've not come across it and was pretty fixed on using Bluetooth, because I'm not sure I will have internet at the location where my project setup will be
You don't need the internet, your Raspberry can be a self contained WiFi AP and network router.
https://www.raspberrypi.org/documentati ... s-point.md

Your Arduino will need an ESP8266 acting as a serial to WiFi modem for the WiFi piece.

MQTT (Message queuing telemetry transport) is the IoT lightweight protocol invented (with a colleague) by the guy on the Isle of Wight with the internet connected mouse traps in his attic.
https://www.hivemq.com/mqtt-essentials/

It's a simple way to get one-to-one, one-to-many, many-to-one and many-to-many messages flowing round your network. With MQTT-SN (MQTT for Sensor Networks) as an extension for devices that have an unreliable connection (through BTLE, Zigbee, Zwave or WiFi).
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
Douglas6
Posts: 4739
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Arduino motor control from Pi 3B+

Mon Jul 15, 2019 10:56 pm

If you wish to continue with Bluetooth, you'll need to be more specific about your issue. You say you can send messages from the Pi to the "Arduino serial monitor". That implies to me that you are sending them to the Arduino. So instead of printing them to the Arduino's serial port, you can test them in code and use the results to control motors, right?

archipi
Posts: 10
Joined: Mon Jun 03, 2019 11:09 am

Re: Arduino motor control from Pi 3B+

Tue Jul 16, 2019 10:17 am

Douglas6 wrote:
Mon Jul 15, 2019 10:56 pm
If you wish to continue with Bluetooth, you'll need to be more specific about your issue. You say you can send messages from the Pi to the "Arduino serial monitor". That implies to me that you are sending them to the Arduino. So instead of printing them to the Arduino's serial port, you can test them in code and use the results to control motors, right?
This is where I am stuck I think. I can see the text I send from the Pi but I don't know how to send command, as they just show up as text on the Arduino. I'm also not sure how to integrate the communication on the Pi into a Python script, as I am currently sending the messages from the Terminal window directly. The Python script should be: "If Button A is pressed, send this code (turns on motor) to the Arduino"

archipi
Posts: 10
Joined: Mon Jun 03, 2019 11:09 am

Re: Arduino motor control from Pi 3B+

Tue Jul 16, 2019 10:35 am

DougieLawson wrote:
Mon Jul 15, 2019 6:58 pm
archipi wrote:
Mon Jul 15, 2019 3:47 pm
Could you elaborate on what that is? I've not come across it and was pretty fixed on using Bluetooth, because I'm not sure I will have internet at the location where my project setup will be
You don't need the internet, your Raspberry can be a self contained WiFi AP and network router.
https://www.raspberrypi.org/documentati ... s-point.md

Your Arduino will need an ESP8266 acting as a serial to WiFi modem for the WiFi piece.

MQTT (Message queuing telemetry transport) is the IoT lightweight protocol invented (with a colleague) by the guy on the Isle of Wight with the internet connected mouse traps in his attic.
https://www.hivemq.com/mqtt-essentials/

It's a simple way to get one-to-one, one-to-many, many-to-one and many-to-many messages flowing round your network. With MQTT-SN (MQTT for Sensor Networks) as an extension for devices that have an unreliable connection (through BTLE, Zigbee, Zwave or WiFi).
Thank you, I will do some research into MQTT communication. Do you have a recommended ESP8266 for the Arduino? I've seen there are a lot of different ones out there

User avatar
Douglas6
Posts: 4739
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Arduino motor control from Pi 3B+

Tue Jul 16, 2019 12:00 pm

archipi wrote:
Tue Jul 16, 2019 10:17 am
This is where I am stuck I think. I can see the text I send from the Pi but I don't know how to send command, as they just show up as text on the Arduino. I'm also not sure how to integrate the communication on the Pi into a Python script, as I am currently sending the messages from the Terminal window directly. The Python script should be: "If Button A is pressed, send this code (turns on motor) to the Arduino"
Use the PySerial library to write to the serial port in Python.

Ypu can only send text, not 'commands', whatever they are. For example, on the Pi side, send the text 'button A was pressed'. On the Arduino side, read the serial data. Test it. If it is equal to 'button A was pressed', then do whatever you need to do to turn the motor on. It will be easier to abbreviate your messages; e.g. 'A1' means 'button A was pressed', 'A0' means 'button A was released', etc.

gordon77
Posts: 4111
Joined: Sun Aug 05, 2012 3:12 pm

Re: Arduino motor control from Pi 3B+

Tue Jul 16, 2019 4:47 pm

At the end of this thread https://www.raspberrypi.org/forums/view ... p?t=236598 you will see an example controlling servos connected to an arduino, by sending text strings as Douglas6 described.

Return to “Automation, sensing and robotics”