Julien Vanier
Published © MIT

Poor Man's CAN Bus

Learn how to connect microcontrollers together using the CAN bus by using a simple AND gate.

BeginnerProtip1 hour4,721
Poor Man's CAN Bus

Things used in this project

Story

Read more

Schematics

Poor Man's CAN circuit

Poor Man's CAN schematics

Code

CAN receiver code

Arduino
Flash to the Photon with the button
const auto ledPin = A3;
const auto ledPowerPin = A6;
CANChannel can(CAN_D1_D2);
const auto canSpeed = 500000;

void setup() {
  pinMode(ledPowerPin, OUTPUT);
  digitalWrite(ledPowerPin, HIGH);
  pinMode(ledPin, OUTPUT);
  can.begin(canSpeed);
}

void loop() {
  CANMessage message;

  while(can.receive(message)) {
    if (message.id == 0x100) {
      digitalWrite(ledPin, !message.data[0]);
    }
  }

  delay(100);
}

CAN transmitter code

Arduino
Flash to the Photon with the LED
const auto buttonPin = D4;
const auto buttonPowerPin = D6;
CANChannel can(CAN_D1_D2);
const auto canSpeed = 500000;

void setup() {
  pinMode(buttonPowerPin, OUTPUT);
  digitalWrite(buttonPowerPin, HIGH);
  pinMode(buttonPin, INPUT_PULLDOWN);
  pinMode(D7, OUTPUT);
  can.begin(canSpeed);
}

void loop() {
  bool button = digitalRead(buttonPin);
  digitalWrite(D7, button);
  CANMessage message;

  message.id = 0x100;
  message.len = 1;
  message.data[0] = button;

  can.transmit(message);

  delay(100);
}

Code and schematics

Credits

Julien Vanier
10 projects • 64 followers
Contact

Comments

Please log in or sign up to comment.