CAN Communication
CAN Communication
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:
MCP2515 module
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
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
4
4. Humidity/Temperature Sensor Connections to Arduino
6 LCD – D5
7 LCD – D6
8 LCD – D7
5V LCD – A
GND LCD – K
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
//#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);
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
10
CAN Receiver Code (Arduino UNO):
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];
11
Output
12