Skip to content
MagggePublic

About

Arduino based can bus for digital model railroad

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

 

History

29 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MobaBus

MobaBus is an arduino based system to connect turnouts, sensors, lights and other model railroad accessories over can-bus. You can build custom components with sensors, accessories and others combined on one Arduino, up to 16 modules per Arduino, or flash one universal sketch and configure the board on the web page of MobaStation. The MobaStation command station drives MobaBus modules with the Z21 app, the multiMAUS or a control software like Rocrail.

Modules

Module Address What it does
MobaBus_Output 1 accessory digital output, direction 1 = on
MobaBus_Output_2pin 1 accessory turnout with two coils or a bistable relay, switched off again after a time
MobaBus_Servo 1 accessory turnout servo on an Arduino pin, speed and angles adjustable
MobaBus_ServoPCA 16 accessories 16 turnout servos on a PCA9685 board
MobaBus_Sensor 1 feedback module 8 inputs (occupancy detectors, buttons), sent as one R-Bus feedback byte
MobaBus_House 1 accessory lively house: a permanent light and rooms that switch on and off at random, a room can be a TV
MobaBus_Flicker 1 accessory candle or fire, optionally with a second LED (eg. red embers)
MobaBus_Blink 1 accessory flashing light, or two lights flashing alternately (level crossing), optionally fading like a bulb
MobaBus_Light 1 accessory dimmable LED with on and off brightness, fades like a bulb if wanted
MobaBus_Universal up to 16 modules builds the modules of a board from a configuration that is set over the bus, see below

Accessories are switched like turnouts: direction 1 (green/straight) = on, 0 = off. Turnout positions and the on/off state of lights survive a restart when MobaBus uses the EEPROM.

Addresses

The easiest way is the address in the sketch:

MobaBus mobaBus;                        // without EEPROM, button and status LED
MobaBus_Servo turnout(7, 70, 110, 230, 100);

void setup() {
  mobaBus.begin();
  mobaBus.attachInterface(&can);
  turnout.setAddress(1001);             // the sketch wins over the EEPROM
  mobaBus.attachModule(&turnout);
}

All settings (angles, speed, times) come from the sketch. The EEPROM keeps addresses, states and settings that were changed over the bus (see CVs below).

MobaBus mobaBus(0) uses the EEPROM from address 0 without button and status LED, for boards where every module has a sketch address or the configuration comes over the bus (see examples/nano_module and examples/universal). The code for the button is not linked then, about 1 KB less on a Nano. Before, MobaBus(0) meant button on pin 8 and LED on pin 9, write MobaBus(0, 8, 9) for that now.

With MobaBus mobaBus(EEPROMstart, progPin, statusLed) modules without sketch address can get their address with a button (to GND) and a status LED. This allows one firmware for many identical boards:

  • Module without address: the LED blinks (feedback 1 s, accessory 0.5 s). Hold the button 5 s (fast blinking), then switch the wanted turnout address in the app (feedback modules: "program R-Bus module"). Modules without address are addressed one after another, the others work meanwhile.
  • New address: hold the button 5 s in normal operation. The LED blinks the number of the offered module (3 blinks, pause = module 3). A short press offers the next one, the next switched address is taken, after 30 s without command the module keeps its address. Modules with a sketch address are not offered.
  • Reset: hold the button 10 s. Only the MobaBus part of the EEPROM (MOBABUS_EEPROM_SIZE bytes from EEPROMstart) is erased, the rest belongs to the sketch.

A programming pin that is LOW already at the start is ignored until it was released once. Never use pins 8 and 9 with the Arduino Motor Shield, these are its brake pins.

Configuration over the bus (CVs)

Settings can be changed while the layout runs, on the MobaBus page of the MobaStation web interface (ESP firmware), with "POM accessory" (programming on the main for accessory decoders) of a control software or with extras/z21test.py of MobaStation, eg. z21test.py 192.168.0.111 cv 1020 33 50. MobaStation forwards it to the module with this address (CONFIG_WRITE/CONFIG_READ on the bus). Feedback modules (MobaBus_Sensor) are only reached over the web interface, the Z21 protocol has no CV command for R-Bus modules.

Written values are stored in the EEPROM and win over the sketch until CV8 = 8, they survive a new upload of the sketch. (With 16 instead of 10 module slots the CVs moved in the EEPROM: values written with an older MobaBus fall back to the sketch once.) The EEPROM slot of a module remembers the module type: when the sketch puts another module type into the slot (eg. modules added or reordered), its old CVs are dropped and the new module starts with the sketch values.

CV Meaning
1, 9 address low/high byte, the new address is used after CV9 was written
7 MobaBus version (read only)
8 manufacturer 13 (DIY), write 8 = back to the values of the sketch
30, 31 type id and number of addresses of the module (read only)

Module CVs (times in 10 ms or 10 s steps, brightness 0..255):

Module CV33 CV34 CV35 CV36 CV37 CV38 CV39 CV40
Servo, ServoPCA angle 0 angle 1 speed auto off (10 ms)
Sensor inverted inputs (bit 0 = input 1)
Output inverted (0/1)
Output_2pin auto off (10 ms) inverted (0/1)
House lit min (10 s) lit max dark min dark max startup shutdown brightness max min (darkest TV scene)
Flicker brightness max 0 candle, 1 fire brightness min
Blink period (10 ms) fade (10 ms) brightness max (on) min (off phase)
Light brightness on brightness off fade (10 ms)

The brightness CVs work on dimmable outputs (PWM pin or PCA9685), an output without PWM is simply on. The effects scale their levels into min..max: a flame never gets darker than min, a bulb with min > 0 glows on.

A changed servo angle is taken at once when the servo stands in this position, so a turnout can be adjusted while watching it. MobaBus::discover() asks all modules for an ANNOUNCE (type, address, type id), the base for a module list.

Universal module

One sketch for every board (examples/universal), what hangs on which pin is set on the MobaBus page of the MobaStation web interface ("Konfigurieren") and stored in the EEPROM of the board, also the addresses. Something added later only has to be entered there. The board is found with "Module suchen" by its board id, a random number made at the first start (from the RAM content after power on and the noise of the analog inputs). Boards answer the search after a random wait; a board that sees a packet with its own id from another board takes a new id and announces itself again, the web page then searches once more.

MobaBus mobaBus(0);
MobaBus_CAN can(10, CAN_125KBPS, MCP_8MHZ, 2);
MobaBus_Universal board(mobaBus);

void setup() {
  mobaBus.begin();
  mobaBus.attachInterface(&can);
  board.begin();
}
Function IOs Needs
Digital (on/off) 1 output (also a MCP23017 pin)
LED dimmable (MobaBus_Light) 1 PWM
Candle, fire (MobaBus_Flicker) 1, fire 1-2 PWM
Flashing light, alternating flasher (MobaBus_Blink) 1 / 2 output (also a MCP23017 pin)
House (MobaBus_House) 1-8, the first one can be the permanent light, rooms can be TVs output (also a MCP23017 pin), PWM for a TV
Servo 1 pin, or channel of a PCA9685 in servo mode
Sensors (MobaBus_Sensor, one feedback module) 1-8 input, all on the Nano or all on one MCP23017

IOs on an Arduino Nano with CAN module: D3-D9 and A0-A3 (PWM: D3, D5, D6, D9), A6/A7 as inputs only (no pull-up). Up to two PCA9685 boards at 0x40 and 0x41 on A4/A5 add 16 channels each. A PCA9685 has one frequency for all channels, so a board is either for lights (1 kHz) or for servos (50 Hz), set on the web page. A servo on a Nano pin takes timer 1, D9 and D10 have no PWM then. Up to two MCP23017 at 0x20 and 0x21 add 16 digital pins each (inputs and outputs, no PWM, GPA7/GPB7 outputs only), shown as "MCP1 A0" … "MCP1 B7" on the web page. Both chip types share the I2C bus on A4/A5.

Limits of a Nano: 16 functions, the modules share 640 bytes of RAM (an LED takes 40 bytes, a candle 56, a house 145, a sensor module about 75), a configuration that does not fit is refused. A function that cannot be built (IO used twice, no PWM, too many modules) is reported on the web page, the others work. Brightness, times and angles are the CVs of the modules.

The configuration is read and written with BOARD_READ/BOARD_WRITE and the board id (registers in MobaBus_Universal.h).

The sketch fills the flash of a Nano almost completely. The messages of the library on Serial are therefore off on the Nano (-DMOBABUS_LOG=1 switches them on), and make upload-nano also switches off the debug output of the mcp_can library (29.1 KB of 30 KB). Built in the Arduino IDE it takes 30.3 KB, which still fits.

Examples

Example What it shows
universal one sketch for every board, configured on the web page of MobaStation
nano_module a complete module on a Nano: servo turnout, house with TV, fire, light and 8 sensors, no button needed
servo servo turnout with programming button and status LED
servo16x 16 servo turnouts on a PCA9685
standardIO outputs, a double coil turnout and 8 sensors
house, candle, crossing the light effects on Nano pins
pca_lights all light effects on one PCA9685
mcp23017 16 more pins on a MCP23017: 8 sensors and lights
bus_sniffer prints every packet on the bus

Light effects

MobaBus_House house(MobaBus_LightOutput(4));            // permanent light on pin 4
house.addRoom(MobaBus_LightOutput(5));
house.addRoom(MobaBus_LightOutput(6), ROOM_TV);         // TV needs a PWM pin
house.setRoomTimes(60, 600, 30, 900);                   // lit 1-10 min, dark 0.5-15 min
house.setSwitchTimes(30, 180);                          // on within 30 s, off within 3 min

MobaBus_Flicker candle(MobaBus_LightOutput(3));
MobaBus_Flicker fire(MobaBus_LightOutput(5), MobaBus_LightOutput(6), FLICKER_FIRE);
MobaBus_Blink crossing(MobaBus_LightOutput(4), MobaBus_LightOutput(7), 1000);

Switched on, the permanent light of a house comes on at once and the rooms one after another. Switched off, the rooms go dark spread over the shutdown time and the permanent light goes last.

MobaBus_LightOutput is an Arduino pin or a channel of a MobaBus_PCA9685 board. Dimmed effects (candle, fire, TV, fading) need PWM: on a Nano with CAN module only pins 3, 5, 6 and 9 are left, a PCA9685 adds 16 dimmable channels over I2C (A4/A5). Use a separate PCA9685 for lights, servos need 50-60 Hz and LEDs flicker visibly there. MobaBus_PCA9685 writes the registers itself (no Adafruit library, about 2 KB less flash), MobaBus_PCA9685(0x42, 50) drives servos with MobaBus_Servo(pca, channel, angle0, angle1, speed, autoOff).

MobaBus_PCA9685 lights(0x41);                           // 1 kHz PWM for LEDs
MobaBus_Flicker candle(MobaBus_LightOutput(lights, 5));

An Arduino pin drives at most 20 mA, use a transistor or a ULN2803 for more LEDs.

More pins: MCP23017

MobaBus_MCP23017 adds 16 digital pins over I2C (A4/A5), address 0x20-0x27 with the pins A0-A2 of the chip, up to 8 chips on one bus. Pin 0-7 is GPA0-GPA7, pin 8-15 is GPB0-GPB7. Like the PCA9685 driver it writes the registers itself, no other library is needed.

MobaBus_MCP23017 mcp(0x20);
MobaBus_Sensor detectors(mcp, 0, 1, 2, 3, 4, 5, 6, 8, true);    // feedback module, pull-ups, MOBABUS_NO_PIN = unused
MobaBus_Output street(MobaBus_LightOutput(mcp, 9));             // outputs and light effects on any pin
MobaBus_Blink beacon(MobaBus_LightOutput(mcp, 11));
  • Outputs only switch on and off, the chip has no PWM: dimmed effects become on/off, a light is on from level 128. A pin becomes an output when it is first used.
  • The sensors of one chip share one I2C read every 2 ms, debounced like Arduino pins.
  • mcp.pinMode(), mcp.digitalWrite() and mcp.digitalRead() work like on the Arduino for anything else.
  • Chips made since 2020 can't use GPA7 and GPB7 (pin 7 and 15) as inputs (Microchip datasheet, revision C), use them for outputs.
  • RESET of the chip must be tied to 5V. Each pin drives up to 25 mA, the whole chip up to 125 mA.

Modules on the command station

Packets that are sent on a board also reach the modules of this board, and their answers come back with loop(). So MobaStation can drive modules on its own pins without CAN bus: create them in the sketch of the station after mobaBus.begin(). On the Arduino Mega the pins 11-13 belong to the DCC signal, PWM effects work on 2, 5, 6, 7, 44, 45 and 46. Three effect modules make the loop of the station about 10 % slower.

Interfaces

  • MobaBus_CAN(cs, speed, clock, interruptPin): MCP2515 module. With the interrupt pin the controller is only read when a message waits, 255 = no interrupt pin (polled over SPI). An interface without hardware is not attached. The speed must be the same on all modules and on MobaStation (MOBABUS_CAN_SPEED, default CAN_125KBPS), clock is the crystal of the module (MCP_8MHZ on most cheap boards).
  • MobaBus_UART(stream): text lines #type,address,port,cmd,len,data..., read without waiting.

Development

make test        # host test on the PC (g++, no board needed)
make examples    # compile all examples for the Arduino Mega, the universal sketch also for the Nano
make upload-nano PORT=/dev/ttyUSB0                       # flash examples/universal to a Nano (old bootloader)
make upload-nano SKETCH=examples/nano_module             # another sketch

Libraries needed:
MCP_CAN_lib
Adafruit-PWM-Servo-Driver-Library (only for MobaBus_ServoPCA)

About

Arduino based can bus for digital model railroad

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages