Devices & Components
Arduino Nano R4
push button switch
TJA1050 CAN Bus Transceiver Module
Diagnostic OBD-II Port Adapter Cable OBD2 for Tesla Model 3 & Model Y
Software & Tools
Arduino IDE
Project description
Code
Honks horn on Tesla when button pressed
c
Honks horn on Tesla when button pressed
1#include <Arduino_CAN.h> 2//This Code Honks the horn of a Tesla when a button is pressed on D6 3#define BUTTON_PIN D6 4#define HONK_TIME_MS 50 5#define CAN_ID_HONK 0x273 6bool lastButtonState = HIGH; 7 8void setup() { 9 // Serial is optional; do not block startup 10 Serial.begin(115200); 11 12 pinMode(BUTTON_PIN, INPUT_PULLUP); // button wired D6 → GND 13 14 if (!CAN.begin(CanBitRate::BR_500k)) //Check if CAN started up fine 15 { 16 while (1); // hard fail 17 } 18} 19 20void loop() { 21 bool buttonState = digitalRead(BUTTON_PIN); //Read button 22 23 if (buttonState == LOW && lastButtonState == HIGH) // Detect button press and release 24 { 25 // Build CAN frame 26 CanMsg hornMsg; 27 hornMsg.id = CAN_ID_HONK; // ID273UI_vehicleControl 28 hornMsg.data_length = 8; 29 30 // Zero the 8 bytes 31 for (int i = 0; i < 8; i++) { 32 hornMsg.data[i] = 0; 33 } 34 35 // Bit 61 → byte 7, bit 5 36 hornMsg.data[7] = 0x20; 37 38 // Honk ON 39 CAN.write(hornMsg); 40 41 delay(HONK_TIME_MS);//Horn honks for 42 43 // Honk OFF 44 hornMsg.data[7] = 0x00; 45 CAN.write(hornMsg); 46 Serial.println("Horn triggered"); 47 } 48 lastButtonState = buttonState; //Set previous button state 49}
Documentation
DMC article about the project
DMC article about the project
https://www.dmcinfo.com/blog/40485/arduino-nano-r4-can-protocol/
Comments
Only logged in users can leave comments