0% found this document useful (0 votes)
35 views12 pages

CAN Communication

This document describes a CAN communication experiment between two Arduino nodes. Node 1 acts as the receiver and displays data on an LCD screen. Node 2 acts as the transmitter and reads temperature and humidity from a DHT11 sensor. It sends the data over CAN using an MCP2515 module. The transmitter code reads the DHT11 sensor, packs the temperature into two bytes of an 8 byte message, and transmits it over CAN. The receiver code receives the message, extracts the temperature from bytes 5-6, and displays the value on an LCD screen. Hardware connections and programming code for each node are provided to implement the CAN communication experiment.

Uploaded by

suguamaran m
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views12 pages

CAN Communication

This document describes a CAN communication experiment between two Arduino nodes. Node 1 acts as the receiver and displays data on an LCD screen. Node 2 acts as the transmitter and reads temperature and humidity from a DHT11 sensor. It sends the data over CAN using an MCP2515 module. The transmitter code reads the DHT11 sensor, packs the temperature into two bytes of an 8 byte message, and transmits it over CAN. The receiver code receives the message, extracts the temperature from bytes 5-6, and displays the value on an LCD screen. Hardware connections and programming code for each node are provided to implement the CAN communication experiment.

Uploaded by

suguamaran m
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

CAN COMMUNICATION

With Arduino Example

1
Introduction

This document explains the CAN controller connectivity between two nodes.
The actual connection settings between two CAN nodes are shown below.

Experimental Setup

Hardware requirement

Node 1: (Receiver)
a) Arduino UNO board
b) CAN MCP2515 module with CAN controller MCP2515 and transceiver
MCP2551
c) LCD for data display

Node 2: (Transmitter)
a) Arduino UNO board
b) CAN MCP2515 module with CAN controller MCP2515 and transceiver
MCP2551
c) DH11 Humidity and temperature module

Hardware Connectivity

1. CAN connection
2
Node1 and Node2 are connected by a two-wire CANH and CANL bus.

The connector information for the CAN MCP2515 module is shown in Fig.2

Arduino Nano or Arduino Uno interfaces to the MCP2515 module using the SPI
interface (Serial Peripheral Interface that comprises of the following terminals:

SCLK – SPI clock


SI – Serial Input
SO – Serial Output
SS – Slave Select or Chip Select (CS)

MCP2515 module

Pin Name Use


VCC 5V input
GND Ground
CS SPI SLAVE select pin (Active low)
SO SPI master input slave output lead
SI SPI master output slave input lead
SCLK SPI clock pin
INT MCP2515 interrupt pin

2. Arduino – MPC2515 – DHT11 Connection (Transmitter)

The following table shows the connectivity of the Arduino board to the
MCP2515.

3
Arduino Uno Component - PIN
(1)
5V MPC2515 – VCC
GND MPC2515 – GND
D10 (SPI_SS) MPC2515 – CS
D12 (SPI_MISO) MPC2515 – S0
D11 (SPI_MOSI) MPC2515 – S1
D13 (SPI_CLK) MPC2515 – SCK
D2 MPC2515 – INT
5V DHT11 – VCC
GND DHT11 – GND
3 (Digital) DHT11 – OUT

Arduino Nano connections to MCP2515 module

CAN controller module. Note the following connections for SPI protocol:
a) SCK pin of the MCP 2515 CAN controller module is connected to SCK pin
of the Arduino board.
b) The SO (Serial Output) of MCP 2515 CAN controller board needs to be
connected to the MISO (Master Input Slave Output) pin in Arduino.
c) SI (Slave input pin) of MCP 2515 CAN Controller needs to connect to
MOSI (Master Output Slave input) of Arduino board.
d) CS (Chip Select ) of CAN controller is connected to SS (Slave Select in
the Arduino board.

The SPI connections are similar for both nodes. For the two nodes the bus
connections are as follows:
a) CANH is connected to CANH
b) CANL is connected to CANL

MCP2515 (Arduino MCP2515 (Arduino


UNO) UNO)
H L
L H
L – CAN high
H – CAN low

CAN Bus connections

4
4. Humidity/Temperature Sensor Connections to Arduino

DHT11 Humidity/Temperature Sensor Interface

The humidity/ temperature sensor is connected to Node 2 controller. Though


Humidity/Temperature are analog parameters, the DHT11 module converts
this into a digital stream of data that can be easily read by the Arduino using
its library.
Once the Arduino board wants to read the DHT11 sensor the following
sequence of operations occur:
a) Arduino board sends a request pulse
b) DHT11 sensor sends five bytes of data in the following format
1) First byte is Integer value of Humidity in binary
2) Second byte is Decimal value of Humidity
3) Third byte is Integer value of Temperature
4) Fourth byte is Decimal value of Temperature
5) Fifth byte is Check sum ( Sum of previous four bytes truncated to one
byte)
Refer to the datasheet for the specific manner in which each logical high bit or
low bit is defined and the method of the request pulse.
5
The Arduino library is able to read the entire stream of bytes with a simple
library command.

3. Arduino – MPC2515 – RG1602A Connection (Receiver)

Arduino Uno (2) Component - PIN


5V MPC2515 – VCC
GND MPC2515 – GND
D10 (SPI_SS) MPC2515 – CS
D12 (SPI_MISO) MPC2515 – S0
D11 (SPI_MOSI) MPC2515 – S1
D13 (SPI_CLK) MPC2515 – SCK
2 MPC2515 – INT
GND LCD – VSS
5V LCD - VDD
10K potentio mtr. LCD – V0
3 LCD – RS
GND LCD – RW
4 LCD – E
5 LCD – D4

6 LCD – D5
7 LCD – D6
8 LCD – D7
5V LCD – A
GND LCD – K

Arduino Uno(2) Connections to LCD and CAN Module

Program Objective
1) Read the TEMPERATURE and humidity from the DH11 sensor using the
Arduino Nano board which is part of CAN node 2.
2) Send the data from Arduino UNO to CAN controller module in Node 2.
Configure the CAN controller module and initiate the CAN controller to
send the data in CAN format using the first two bytes of 8 byte data
payload. Choose an appropriate identifier.
3) Data is transmitted from Node 2 at 1 second intervals into the CAN bus
4) Node 1 receives the data, verifies the data and parses the data from the
8-byte data stream into two variables
5) Node 1 proceeds to display the data of temperature in the LCD display.

Notes:
Arduino Nano configures the CAN controller in Node 2 using SPI interface at
9600 baud rate

6
Arduino Uno configures the CAN controller in Node 1 using SPI interface at
9600 baud rate.
Both the CAN controllers are configured to send and receive data in the
CAN bus at 500Kbps data rate

7
Exercise for assignment

The J1939 protocol document provides the following information for Cabin interior
temperature measurement.
SPN: 168
PGN: 65269
CAN ID: 0xFEF5
Byte Position: 5-6
Resolution: 1 deg/bit
Offset: 0
Min : 0
Max: 50
Units: Deg C
Assume that a DHT11 sensor is interfaced to the Arduino controller to Board 1
to input temperature and humidity. Send the data of this temperature in a 8-byte
frame so that the temperature can be expressed as a 16-bit value occupying bytes 5
and 6 with the byte 5 being the lowest significant byte.
The data should be read by CAN board 2 and displayed in a LCD in the range of 0 to
50 deg C. You may ignore the value after the decimal for display purposes.
Write programs for the following
a) Board 1 should read the DHT11 interfaced to Arduino Uno board, read the
temperature and load the value as two bytes in Byte 5 and Byte 6 in a 8-byte
array. Send the data using CAN protocol with CAN ID of 0xFEF5 (alternatively
use a 11 bit ID of your choice).
Board 2 (Arduino Uno ) should read this array of data, convert the data from
Bytes 6 and Byte 5 and store in two variables. Display the integer value of the
temperature in the LCD. Decimal value for temperature may be ignored.
b) Display the temperature in the first line. Display the CAN ID of the Transmitter
node used in second line of LCD.
c) Test the programs remotely by sending your code to the instructor/TA where
the kits are located and view the LCD output.
Bootcamp assignment (Submit one single Word document)
a) Draw the schematic circuit diagram neatly for Exercise 1
b) Draw a neat logic waveform of the DHT11 sensor output if the temperature
is 27 deg C (Refer to datasheet DHT11 to understand the format). Timing
should be clear in the waveform.
c) Enclose the text of the Receiver and Transmit code inside the Word
document. Comment the code as much as possible.
d) Make sure your name and Roll no are there in the submitted document

8
Hardware Connections with both nodes using Arduino Uno board

9
Code Used

CAN Transmitter Code (Arduino Nano)


#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
int SensePin=3;
struct can_frame canMsg;
MCP2515 mcp2515(10);
DHT dht(SensePin,DHTTYPE); //initilize 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 sesnor value
delay(500);

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] = 0x00; //Update humidity value in [0]
canMsg.data[1] = 0x00; //Update temperature value in [1]
canMsg.data[2] = 0x00; //Rest all with 0
canMsg.data[3] = 0x00;
canMsg.data[4] = 0x00;
canMsg.data[5] = t;
canMsg.data[6] = 0x00;
canMsg.data[7] = 0x00;
mcp2515.sendMessage(&canMsg); //Sends the CAN message
delay(1000);
}

10
CAN Receiver Code (Arduino UNO):

#include <SPI.h> //Library for using SPI Communication


#include <mcp2515.h> //Library for using CAN Communication
#include <LiquidCrystal.h> //Library for using LCD display

const int rs = 3, en = 4, d4 = 5, d5 = 6, d6 = 7, d7 = 8;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7); //Define LCD display pins RS,E,D4,D5,D6,D7
struct can_frame canMsg;
MCP2515 mcp2515(10); // SPI CS Pin 10

void setup() {
lcd.begin(16,2); //Sets LCD as 16x2 type
lcd.setCursor(0,0); //Display Welcome Message
lcd.print("SUGUMARAN-65030");
lcd.setCursor(0,1);
lcd.print("BITS PILANI");
delay(3000);
lcd.clear();
SPI.begin(); //Begins SPI communication
Serial.begin(9600); //Begins Serial Communication at 9600 baudrate
mcp2515.reset();
mcp2515.setBitrate(CAN_500KBPS,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) // To receive data (Poll Read)
{
int x = canMsg.data[5];
//int y = canMsg.data[1];

lcd.setCursor(0,0); //Display Temp & Humidity value received at 16x2 LCD


//lcd.print("Humidity : ");
//lcd.print(x);
lcd.setCursor(0,0);
lcd.print("Temp : ");
lcd.print(x);
delay(1000);
lcd.clear();
}
}

11
Output

12

You might also like