Printed from www.nzmeccano.com
Controlling Smart Servos with an Arduino
(Author: David Couch)
Controlling Smart Servos with an Arduino
In 2015 three documents were made available on the Meccano web site. They are no longer available there, and so if you would like them please send me an email at david.couch46@gmail.com One is a file named Meccano_SmartModuleProtocols_2015.pdf, which is a description of the communication protocol between a computer, the servos and the LED Module. The other is a file named meccanoid-library.zip. It contains two files named MeccaBrain.cpp and MeccaBrain.h, which are an Arduino library of code to implement the protocol and control the servos and LED module.
Installing the library files
Wayne Hortensius has kindly provided an additional file named keywords.txt. It is not essential but it is helpful. Its function is to make the keywords, mainly function names, in the library appear in a different colour when you edit code. The file is appended to this document. Select the text with the mouse, copy and paste it into Wordpad or another editor, and save it as keywords.txt in the new MeccaBrain folder.
Connecting the servos to the Arduino
The protocols document includes a diagram showing how a servo is connected to an Arduino and a separate power supply. Two resistors are required. I built this simple circuit on an Adafruit Proto Screw Shield, as shown in fig. 1. The positive power supply lead and servo power lead (both red) are connected to the W terminal. A 22k resistor is soldered to the pad for this terminal and to another pad. A 1k resistor is soldered between this pad and a second pad. A wire connects the second pad to the pin 12 terminal. Another wire connects the first pad to the pad for the X terminal. The yellow or white data wire of the servo is connected to the X terminal. The power supply negative lead and the servo ground lead (both black) are connected to the ground terminal.
An alternative would be to solder a three-pin header to the shield, with the required connections, and plug the servo plug on to it, with an extension lead if required.
The example sketch
The sketch does the following:
(1) Establish the communication with the servos. The Serial Monitor displays the values sent to and received from the servos.
(2) Move each servo back and forth three times.
(3) Cycle through the colours of the LEDs in the servos.
(4) Put the second servo in LIM mode and repeatedly read and display its position. If you move the servo by hand you will see the values changing.
For a copy of the sketch file please send email to david.couch46@gmail.com
Module data
FF Header byte
FE ID not assigned
FD Erase ID (resets Smart Module)
FC Request module type
FB Reserved
FA Set LIM mode
Servo colour setting
F7 White
F6 Cyan
F5 Magenta
F4 Blue
F3 Yellow
F2 Green
F1 Red
F0 Off
Values from 00 to EF are defined as position data. However the library will send only values from 18 to E8. The range of reported position values I have seen is 04 to E9.
The file keywords.txt
# Syntax Coloring Map for MeccaBrain
#######################################
#######################################
# Datatypes (KEYWORD1)
#######################################
MeccaBrain KEYWORD1
#######################################
# Methods and Functions (KEYWORD2)
#######################################
outputByteInfo KEYWORD2
inputByteInfo KEYWORD2
inputBytesInfo KEYWORD2
checkSumByteInfo KEYWORD2
servoPositionsInfo KEYWORD2
moduleTypeInfo KEYWORD2
moduleNumInfo KEYWORD2
getLEDorder KEYWORD2
getLEDbyte1 KEYWORD2
getLEDbyte2 KEYWORD2
setLEDColor KEYWORD2
setServoColor KEYWORD2
setServoPosition KEYWORD2
setServotoLIM KEYWORD2
getServoPosition KEYWORD2
communicate KEYWORD2
sendByte KEYWORD2
receiveByte KEYWORD2
calculateCheckSum KEYWORD2
#######################################
# Constants (LITERAL1)
#######################################
If you have any interesting information or comments about this page, please add them here: