Project showcase
CAN Bus Using Arduino

CAN Bus Using Arduino © GPL3+

We implement CAN Bus communication with MCP2515 module to communicate between two Arduino for sending temperature data from DHT

  • 11 views
  • 0 comments
  • 0 respects

Components and supplies

Apps and online services

About this project

CAN Bus

CAN Bus, stand for Controller Area Network, is one type of serial communication that usually used in industrial and automotive environments. The real example of CAN Bus application can be found in speed car data sensor that can be transferred to the rpm indicator.

CAN Bus is a message based protocol that can be used for multiple device communication. The figure below represents that when several CAN devices are connected together like a network, each device can communicate with other devices in the node. In general, CAN communication range is in range 50Kbps to 1Mbps, with the distance range is 40 meters (at 1 Mbps) to 1000 meters (at 50 kbps).

In this communication, data is transferred in a certain message format. Each message consists of many segments, but there are two main segments: Identifier and Data. Identifier or CAN ID, or known as Parameter Group Number (PGN) is used to identify the CAN device in the CAN network, usually in 11 (Standard CAN) or 29 bit (Extended CAN) length, based on the CAN protocol. While Data is the message content to be sent, usually in 0 to 8 bytes length.

CAN Protocol consists of two wires : CAN_H and CAN_L to send and receive the message. This two wires act as a differential line where CAN signal is represented with the potential difference between them. If the difference is positive and larger than a certain minimum voltage, then the signal is 1, and if the difference between them is negative, it will be 0.

Usually, in CAN communication, twisted pair cable is used. And at the ends of the CAN networks, a single 120-ohm resistor is used. It is because the line should be balanced and tied to the same potential.

In this project, we are going to implement CAN Bus communication with MCP2515 module to communicate between two Arduino for sending temperature data from DHT11 sensor. This module uses 5V as operating voltage and has pinout configuration as shown in the following table.

The result is as shown below:


Reference:https://circuitdigest.com/microcontroller-projects/arduino-can-tutorial-interfacing-mcp2515-can-bus-module-with-arduino

https://www.allaboutcircuits.com/technical-articles/introduction-to-can-controller-area-network/

Code

TransmitterC/C++
#include <SPI.h> //Library for using SPI Communication
#include <mcp2515.h> //Library for using CAN Communication
#include <DHT.h> //Library for using DHT sensor
#define DHTPIN A0
#define DHTTYPE DHT11

struct can_frame canMsg;
MCP2515 mcp2515(10);
DHT dht(DHTPIN, DHTTYPE); //initialize object dht for class DHT with DHT pin with STM32 and DHT type as DHT11

void setup(){
  while (!Serial);
  Serial.begin(9600);
  SPI.begin(); //Begins SPI communication
  dht.begin(); //Begins to read temperature & humidity sensor value
  mcp2515.reset();
  mcp2515.setBitrate(CAN_500KBPS, MCP_8MHZ); //Sets CAN at speed 500KBPS and Clock 8MHz
  mcp2515.setNormalMode();
}

void loop(){
  int h = dht.readHumidity(); //Gets Humidity value
  int t = dht.readTemperature(); //Gets Temperature value
  canMsg.can_id = 0x036; //CAN id as 0x036
  canMsg.can_dlc = 8; //CAN data length as 8
  canMsg.data[0] = h; //Update humidity value in [0]
  canMsg.data[1] = t; //Update temperature value in [1]
  canMsg.data[2] = 0x00; //Rest all with 0
  canMsg.data[3] = 0x00;
  canMsg.data[4] = 0x00;
  canMsg.data[5] = 0x00;
  canMsg.data[6] = 0x00;
  canMsg.data[7] = 0x00;
  mcp2515.sendMessage(&canMsg); //Sends the CAN message
  delay(1000);
}
ReceiverC/C++
#include <SPI.h> //Library for using SPI Communication 
#include <mcp2515.h> //Library for using CAN Communication

struct can_frame canMsg; 
MCP2515 mcp2515(10); // SPI CS Pin 10 

void setup() {
  SPI.begin();   //Begins SPI communication
  Serial.begin(9600); //Begins Serial Communication at 9600 baud rate 
  mcp2515.reset();                          
  mcp2515.setBitrate(CAN_50KBPS,MCP_8MHZ); //Sets CAN at speed 500KBPS and Clock 8MHz 
  mcp2515.setNormalMode();  //Sets CAN at normal mode
}

void loop(){
 if ((mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK) && (canMsg.can_id == 0x036)){
     int x = canMsg.data[0];         
     int y = canMsg.data[1];  
     Serial.print("Kelembaban: ");
     Serial.print(x);
     Serial.print(" Suhu: ");
     Serial.println(y);      
   }
}

Schematics

CAM Bus Circuit
For Transmitter (Arduino 1)
MCP2515 Module / DHT Sensor -> Arduino Nano
MPC2515 – VCC -> +5V
MPC2515 – GND -> GND
MPC2515 – CS -> D10 (SPI_SS)
MPC2515 – SO -> D12 (SPI_MISO)
MPC2515 - S I -> D11 (SPI_MOSI)
MPC2515­ – SCK -> D13 (SPI_SCK)
MPC2515 – INT -> D2
DHT11 – VCC -> +5V
DHT11 – GND -> GND
DHT11­ – OUT -> A0

For Receiver (Arduino 2)
MCP2515 Module -> Arduino Uno
VCC -> +5V
GND -> GND
CS -> 10 (SPI_SS)
SO -> 12 (SPI_MISO)
SI -> 11 (SPI_MOSI)
SCK -> 13 (SPI_SCK)
INT -> 2

Between two MCP2515
MCP2515 on Arduino Nano (Arduino 1) -> MCP2515 on Arduino Nano (Arduino 2)
H -> H
L -> L
Background gvsjaozkvr

Comments

Created by

Maurizfa (13216008) / Arthur Jogy (13216037) / Agha Maretha (13216095)

Are you Maurizfa (13216008) / Arthur Jogy (13216037) / Agha Maretha (13216095)? Claim this project and add it to your profile.

This is mine

Posted by

Default
Houmous Cost
  • 0 projects
  • 0 followers

Published on

May 17, 2020
See similar projects
you might like

Similar projects you might like

Architectural Model of a Bus Stop with Automatic Sunshade

Project showcase by addicttux

  • 5,753 views
  • 25 comments
  • 40 respects

Arduino Bluetooth Robot for Android Device

Project showcase by aip06

  • 5,101 views
  • 2 comments
  • 19 respects

Intelligent Transportation System In City Bus Shelter

Project in progress by Team Technoid

  • 4,782 views
  • 0 comments
  • 13 respects

ESP8266 ESP-01 Webserver

by Robin Kanattu Thomas

  • 133,211 views
  • 22 comments
  • 164 respects

Morse Code Translator

Project showcase by Team ONYX

  • 22,983 views
  • 10 comments
  • 25 respects

Attiny85/84 with Bluetooth

by Robin Kanattu Thomas

  • 19,774 views
  • 1 comment
  • 56 respects
Add projectSign up / Login