Blog 19

From BitWizard WIKI
Revision as of 13:44, 23 November 2015 by Cartridge1987 (talk | contribs) (How to change SPI address, when the same addresses are connected on 1 port (Raspberry Pi))

Jump to: navigation, search

2 Wheeled car: Arduino Version

#define Motor 0x48

#include <Wire.h> 

//Time to pause rotating
//static unsigned long Pause = 10000;
static unsigned long Speed = 40;

void setup()
  Wire.begin(); // wake up I2C bus

byte get_var(byte address, byte reg)
  byte value;   
  delayMicroseconds (10);
  Wire.requestFrom(Motor, 1);
  value =;
  return value;

void set_var(byte address, byte reg, byte value)
  Wire.beginTransmission(address); // transmit to motor     
  delayMicroseconds  (10);
  delayMicroseconds  (10);
  Wire.endTransmission();    // stop transmitting

void loop()
  unsigned long AddressA;
  unsigned long AddressB;  

  char buf[32];
  set_var(0x48, 0x21, Speed); 
  set_var(0x48, 0x31, Speed);  

  AddressA = get_var(0x48, 0x21);
  sprintf (buf, "Speed:  A:%d ", AddressA);
  Serial.write (buf); 

  AddressB = get_var(0x48, 0x31);
  sprintf (buf, " B:  %d\r\n", AddressB);
  Serial.write (buf); 

//  delay(Pause);

This is the list I have made for the Raspberry Pi Version, what also counts for the Arduino version(If your car is switched to the other side this can of course be the opposite):

  • # X is forwards - Y is backwards
  • #Wheels at front
  • #20 A backwards
  • #21 A forwards
  • #22 A stop
  • #30 B backwards
  • #31 B forwards
  • #32 B stop

You can also check the motor protocol, for more info.

In my example script we go forward with 0x21 + 0x31 having the same value. For other movements you should have(If you car is flipped this can of course be the opposite!):

To go backwards:

  set_var(0x48, 0x20, Same Speed value); 
  set_var(0x48, 0x30, Same Speed value); 

To go left:

  set_var(0x48, 0x21, High Speed value); 
  set_var(0x48, 0x31, Low Speed value); 

To go right:

  set_var(0x48, 0x21, Low Speed value); 
  set_var(0x48, 0x31, High Speed value); 

To stop the car:

  set_var(0x48, 0x21, Speed value doesn't matter); 
  set_var(0x48, 0x31, Speed value doesn't matter);

To rotate to the left:

  set_var(0x48, 0x21, Same Speed value); 
  set_var(0x48, 0x30, Same Speed value); 

To rotate to the right:

  set_var(0x48, 0x20, Same Speed value); 
  set_var(0x48, 0x31, Same Speed value);

If you do this don't forget to add: static unsigned long Speed2 = "Put here your value";

How to change SPI address, when the same addresses are connected on 1 port (Raspberry Pi)

Here I want to tell you quickly how to make it possible to connect an 7Fets attached ot an other 7Fets connected to the raspberry Pi. When you normally connect on SPI0 and scan:

bw_tool -S -D /dev/spidev0.0

You will see:

88: spi_7fets 1.1

This is something you don't want, because now he only sees 1 7FETs. And with the car you of course want to make it possible to let the car drive different directions. To do this you first have to remove the 7FETs you got connected to the other 7FETs that is connected to the port. Now you should change the address of the connected 7FETs. ( If you would have both of the 7FETs connected both the addresses will change to the new one ) To change the address you have to do the command:

bw_tool -S -D /dev/spidev0.0 -a 88 -w f1:55 f2:aa f0:70

( If you are using I2C, that works the same only you have to change -S to -I and /dev/spidev0.0 to /dev/spi-i2c-1 )

What the code does:


opens the address


allows changing the address


Is me changing the address to 70. ( You can of course give your own value )

On User Interface you can read more about the commands.

If you then connect the other 7FETs on the other 7FETs again and scan again you will get as result: 70: spi_7fets 1.1 88: spi_7fets 1.1

You can now easily talk to both 7FETs.