L9110 DC Motor Control (B4R,B4J,Node-RED)

Experiments & prototyping with the Arduino.
rwblinn
Site Admin
Posts: 195
Joined: 07 Oct 2017, 12:16

L9110 DC Motor Control (B4R,B4J,Node-RED)

Postby rwblinn » 16 Nov 2017, 10:23

Objectives
Control a DC Motor using the L9110 Motor Control Driver connected to an Arduino.
Test Scenarios
  • Arduino controlling the DC motor by a program developed with B4R.
  • Arduino connected to a PC COM port (USB), controlled by a B4J Application (requires Java).
  • Arduino connected to a Raspberry Pi (USB), controlled by a Node-RED flow.
B4R & B4J are development tools by Anywhere Software.
Node-RED is Flow-based programming for the Internet of Things by the JS Foundation.

Parts Needed
  • 1x Arduino UNO
  • 1x DC Motor
  • 1x L9110 Motor Control Driver
Wiring

Code: Select all

L9110 = Arduino UNO
VCC = 5v
GND = GND
IB1 = not used
IA1 = not used
IB2 = PWM 11 (green)
IA2 = PWM 12 (yellow)

DC Motor = L9110
DC = 0B2
GND = 0A2


Circuit
Image

Overview
Image

Solution Arduino Program (B4R)
The 2 motor pins are set in output mode. The motor drive direction is set by bits HIGH (true) or LOW (false) - DigitalWrite to both IBA2 & IB2 pins.
The speed can have a value between 0 and 255. This is set via AnalogWrite to the IB2 pin.

The Arduino communicates using the serial line (115200:8N1). An asynchronous stream is handles incoming & outgoing data.
The incoming data must be defined as an array of 2 bytes, whereas (0 is the first byte):
Byte 0: Direction - 0=Break (BRK), 1=Forwards (FWD), 2=Reverse (REV)
Byte 1: Speed - value between 0 and 255
Summary:
The applications connected to the Arduino must have the serial line setup as 115200:8N1 and send the data as array of bytes.

Solution PC Application (B4J)
The B4J Application connects to a COM port (115200:8N1). For the initial test, connected to a PC Port COM5.
An asynchronous stream is used to read and handle incoming data via the serial line.
The outgoing data, to control the motor, holds a an array of bytes (0 is the first byte):
Byte 0: 0=BRK, 1=FWD, 2=REV
Byte 1: 0-255 speed
Image

Solution Node-RED
Tested the solution as described, also with Node-RED running on a Raspberry Pi.
The Arduino connects to the Raspberry Pi USB port /dev/ttyUSB0. Hint: check the ports with command: ls /dev/tty*.
A simple flow is defined using two inject nodes and a serial out node.
The serial out node has a properties /dev/ttyUSB0:115200-8N1. For other properties, kept the default.
When Node-RED starts, the log shows an entry like: serial port //dev/ttyUSB0 opened at 115200 baud 8N1. When accessing the flow, the Serial Node shows status connected.

Important(!) is to send the data from Node-RED to the connected Arduino in binary format - array of bytes - as described previous.
For the simple flow, Inject Nodes are used with payload buffer and content in JSON format. Example to let the motor run direction forwards with speed 150 is a payload of [1,150].

Instead of a simple flow, a Node-RED Dashboard flow can be created to control the motor via Browser Dashboard.
To send the binary array of bytes a Function Node is required:
Example of a Function Node (JavaScript) to create a Binary Buffer Payload and send via the Serial Node to the Arduino
direction = 2
speed = 150
//Create an array holding direction and speed
var array = [direction,speed]
//Add the array to a binary buffer payload
msg.payload = Buffer.from(array);
return msg;

Node-RED Simple Flow

Code: Select all

[
{"id":"e882523a.a8af5","type":"serial out","z":"7aa38ac7.9cf4cc","name":"","serial":"70d39577.0b5cb4","x":789.5,"y":92,"wires":[]},
{"id":"a00597ae.89a558","type":"inject","z":"7aa38ac7.9cf4cc","name":"FWD","topic":"","payload":"[1,150]","payloadType":"bin","repeat":"","crontab":"","once":false,"x":99.5,"y":49,"wires":[["e882523a.a8af5"]]},
{"id":"fc6fa63d.84f488","type":"inject","z":"7aa38ac7.9cf4cc","name":"BRK","topic":"","payload":"[0,0]","payloadType":"bin","repeat":"","crontab":"","once":false,"x":99.19999694824219,"y":92.19999694824219,"wires":[["e882523a.a8af5"]]},
{"id":"6186db44.06372c","type":"inject","z":"7aa38ac7.9cf4cc","name":"REV","topic":"","payload":"[2,150]","payloadType":"bin","repeat":"","crontab":"","once":false,"x":97.19999694824219,"y":138.1999969482422,"wires":[["e882523a.a8af5"]]},
{"id":"70d39577.0b5cb4","type":"serial-port","z":"","serialport":"/dev/ttyUSB0","serialbaud":"115200","databits":"8","parity":"none","stopbits":"1","newline":"\\n","bin":"false","out":"char","addchar":false}
]

Note: The enhanced Dashboard flow is included in the download.

Download
Download B4R & B4J source code, Node-RED flow

Software Versions
Raspberry Pi Raspian v9.1, Node-RED v.0.17.5, nodejs v6.11.4, Node-RED Dashboard v2.5.1, B4R v2.20, B4J v5.90.

ToDo
  • Experiment is completed.

Version
20171016

Return to “Arduino”

Who is online

Users browsing this forum: No registered users and 1 guest