3.4. Setting I²C addresses with a Raspberry Pi
If you have one Motoron and it is the only I²C target device in your system, there is no need to change the Motoron’s I²C address and you can skip this section.
Each device on an I²C bus should have a unique address so that you can communicate with the device without interfering with other devices on the bus. By default, the Motoron uses I²C address 16, so if you have connected two or more Motorons to your bus, or you have another device that uses address 16, you will need to change the I²C addresses of one or more Motorons.
If you need to change your Motoron’s I2C address, the recommended procedure for setting the I²C address of one or more Motorons that are connected to the I²C bus of your Raspberry Pi is:
- Ensure that the JMP1 pin on each Motoron is not connected to anything.
- Download the Motoron Motor Controller Python library for Raspberry Pi, and install its dependencies, as described in the “Getting started” section of its README file.
- In a Terminal, use
cdto navigate into the directory holding the library and its examples.
./set_i2c_addresses_example.py. This utility will print some information and then prompt you for a command.
- Perform a scan of the I²C bus by typing “s” and pressing Enter. You should get output that looks like this:
Scanning for I2C devices… Found device at address 0 Found device at address 16 Done.The scan detects that a device on the bus is responding to address 16 because that is the Motoron’s default address. It also detects a device on address 0 because that is the I²C general call address and all Motorons respond to it by default, in addition to the normal address. We will use address 0 to send commands in later steps, so if the scan does not detect any devices on address 0, those steps will probably fail.
- Warning: If you have devices on your I²C bus that are not Motorons, the steps below could cause undesired behavior by sending commands to them that are intended for the Motorons. You might consider removing those devices from your bus temporarily.
- Connect the JMP1 pin of one Motoron to GND. If you have not soldered headers to the JMP1 pin and its adjacent GND pin, you can connect those two pins together using a wire, test clip, or with a shorting block attached to a 1×2 male header. Connecting JMP1 to GND is how we select which Motoron’s address will be changed in the next step. Only one Motoron at a time should have its JMP1 pin connected to GND.
- Set the address of the selected Motoron by typing “a”, followed by the address (in decimal), and then pressing Enter. We recommend picking an address between 8 and 119 that is not in use by any other devices on your bus (numbers outside that range could work too, but they are reserved for other uses by the I²C specification). For example, type “a17” to set the address of the Motoron to 17. Alternatively, you can just send “a” by itself in order to have the program automatically pick an address for you, starting at 17 and skipping addresses that are already in use on the bus.
This sends a “Write EEPROM” command to all of the Motorons using address 0, but the command will only have an effect on the one Motoron whose JMP1 line is low. That Motoron will record the address in its non-volatile EEPROM memory, but will not start using it yet.
- Disconnect the JMP1 pin from GND.
- Repeat the last three steps for every Motoron whose address you wish to change.
- Type “r” to make the new addresses take effect. This sends a “Reset” command to all of the Motorons using address 0. Alternatively, you can power cycle the system or use the RST pin to reset the Motorons.
- Perform another scan of the I²C bus by typing “s”. Check that a device is now found on each of the addresses that you assigned.
- Please note that most of the example code we provide is configured to use address 16. If you have assigned a different address to your Motoron, you will need to configure any code you run to use that same address. For example, when using the Python library, write
mc = motoron.MotoronI2C(address=17)to create a MotoronI2C object that uses address 17.