# Simple Arduino inputs to CANBUS via MCP2515

**URL:** <https://forum.realdash.net/t/simple-arduino-inputs-to-canbus-via-mcp2515/7574>\
**Category:** Custom Hardware / Electronics\
**Created:** [February 13, 2025, 11:35pm UTC](https://forum.realdash.net/t/simple-arduino-inputs-to-canbus-via-mcp2515/7574 "2025-02-13T23:35:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mOS\_cOww](https://forum.realdash.net/letter_avatar_proxy/v4/letter/m/7cd45c/32.png) [@mOS\_cOww](https://forum.realdash.net/u/mOS_cOww)\
**Post date:** [February 13, 2025, 11:35pm UTC](https://forum.realdash.net/t/simple-arduino-inputs-to-canbus-via-mcp2515/7574/1 "2025-02-13T23:35:11Z")

</div>

Hi,

I’ve been trying to set up an Arduino Nano to receive inputs from a voltage source and translate that to CANBUS. I want to take inputs from the cars dashboard such as indicators, high beam, handbrake etc. and get the Arduino to sent the CAN commands to RealDash using the existing RealDash ID’s.

I have successfully made a simple sketch that can simultaneously send the commands to RealDash. I am no programmer so I’m sure there are way better way of doing this but I thought I would share this here as I couldn’t find any examples on the internet.

This works with SEEEDSTUDIO USBCananalyzer and should work with all RealDash compatible CAN devices as far as I know.

I have an Arduino Nano using the Autowp 2515 canbus library to send the can commands via a generic MCP2515. I use generic mini DC DC buck transformers to drop down the voltage from the cars instrument cluster to 5v.

You can use the analogue input for fuel level sending but I haven’t included that in this code.

#include \<SPI.h\>  
#include \<mcp2515.h\>

// Pin Definitions  
#define LEFT\_INDICATOR\_PIN 3  
#define RIGHT\_INDICATOR\_PIN 4  
#define HIGH\_BEAM\_PIN 5  
#define PARKING\_BRAKE\_PIN 7

// Specific CAN Message IDs as provided  
#define LEFT\_INDICATOR\_MSG\_ID 0x160  
#define RIGHT\_INDICATOR\_MSG\_ID 0x161  
#define HIGH\_BEAM\_MSG\_ID 0x157  
#define PARKING\_BRAKE\_MSG\_ID 0x164

struct can\_frame canMsg;  
MCP2515 mcp2515(10); // CS pin for MCP2515

// State tracking for buttons with independent states  
struct ButtonState {  
bool currentState = false;  
bool lastState = false;  
unsigned long lastDebounceTime = 0;  
unsigned long debounceDelay = 50;  
};

ButtonState leftIndicator;  
ButtonState rightIndicator;  
ButtonState highBeam;  
ButtonState parkingBrake;

void setup() {  
Serial.begin(115200);

// Initialize button pins as INPUT for 5V signaling  
pinMode(LEFT\_INDICATOR\_PIN, INPUT);  
pinMode(RIGHT\_INDICATOR\_PIN, INPUT);  
pinMode(HIGH\_BEAM\_PIN, INPUT);  
pinMode(PARKING\_BRAKE\_PIN, INPUT);

// Initialize CAN bus  
mcp2515.reset();  
// Set bitrate to 1000 Kbps (1 Mbps) with 16 MHz crystal  
mcp2515.setBitrate(CAN\_1000KBPS, MCP\_16MHZ);  
mcp2515.setNormalMode();  
}

void sendCanMessage(uint16\_t msgId, bool state) {  
canMsg.can\_id = msgId;  
canMsg.can\_dlc = 1;  
canMsg.data[0] = state ? 0x01 : 0x00;  
mcp2515.sendMessage(&canMsg);  
}

void checkButton(int pin, ButtonState &buttonState, uint16\_t msgId) {  
int reading = digitalRead(pin);

// Check for debounce  
if (reading != buttonState.lastState) {  
buttonState.lastDebounceTime = millis();  
}

if ((millis() - buttonState.lastDebounceTime) \> buttonState.debounceDelay) {  
// If the button state has changed  
if (reading != buttonState.currentState) {  
buttonState.currentState = reading;

```
  // Send CAN message when state changes
  sendCanMessage(msgId, reading == HIGH);
}

```

}

buttonState.lastState = reading;  
}

void loop() {  
// Check and send messages for each button independently  
checkButton(LEFT\_INDICATOR\_PIN, leftIndicator, LEFT\_INDICATOR\_MSG\_ID);  
checkButton(RIGHT\_INDICATOR\_PIN, rightIndicator, RIGHT\_INDICATOR\_MSG\_ID);  
checkButton(HIGH\_BEAM\_PIN, highBeam, HIGH\_BEAM\_MSG\_ID);  
checkButton(PARKING\_BRAKE\_PIN, parkingBrake, PARKING\_BRAKE\_MSG\_ID);

// Small delay to prevent overwhelming the CAN bus  
delay(10);  
}

This should be a good base for any modifications you might need to make it work for you.

Below is the XML for the inputs:

frame id=“0x160” endianess=“big” writeInterval=“100”  
value targetId=“160” offset=“0” length=“1”/  
frame id=“0x161” endianess=“big” writeInterval=“100”  
value targetId=“161” offset=“0” length=“1”/  
frame id=“0x157” endianess=“big” writeInterval=“100”  
value targetId=“157” offset=“0” length=“1”/  
frame id=“0x164” endianess=“big” writeInterval=“100”  
value targetId=“164” offset=“0” length=“1”/  
frame id=“0x170” endianess=“big” writeInterval=“100”  
value targetId=“170” units=“%” offset=“0” length=“1” conversion=“V\*100”/

You will have to modify the formatting to match the RealDash XML formatting.

I hope this helps someone.

---

<div class="post-metadata">

**Author:** ![mmain](https://forum.realdash.net/user_avatar/forum.realdash.net/mmain/32/651_2.png) [@mmain](https://forum.realdash.net/u/mmain)\
**Post date:** [February 14, 2025, 2:18am UTC](https://forum.realdash.net/t/simple-arduino-inputs-to-canbus-via-mcp2515/7574/2 "2025-02-14T02:18:38Z")

</div>

[https://github.com/janimm/RealDash-extras/blob/master/RealDash-CAN/README.md](https://github.com/janimm/RealDash-extras/blob/master/RealDash-CAN/README.md)

The attached RealDash extras has worked examples of doing what you describe. I used an ESP32 to do this previously transmitting the canbus data over Bluetooth to RealDash

---

<div class="post-metadata">

**Author:** ![realdashdev](https://forum.realdash.net/user_avatar/forum.realdash.net/realdashdev/32/6_2.png) [@realdashdev](https://forum.realdash.net/u/realdashdev)\
**Post date:** [February 14, 2025, 7:57am UTC](https://forum.realdash.net/t/simple-arduino-inputs-to-canbus-via-mcp2515/7574/3 "2025-02-14T07:57:29Z")

</div>

> [@mOS\_cOww](#):
>
> I hope this helps someone.

Thank you for sharing!

---

<div class="post-metadata">

**Author:** ![mOS\_cOww](https://forum.realdash.net/letter_avatar_proxy/v4/letter/m/7cd45c/32.png) [@mOS\_cOww](https://forum.realdash.net/u/mOS_cOww)\
**Post date:** [February 15, 2025, 1:57am UTC](https://forum.realdash.net/t/simple-arduino-inputs-to-canbus-via-mcp2515/7574/4 "2025-02-15T01:57:54Z")

</div>

I couldn’t get that to work with the MCP2515 so I gave up on it.
