CAN communication between PIC and Arduino

Summary:

This project helps in understanding the insights of CAN protocol which interfaces two different microcontrollers i.e. PIC and Arduino. Controller Area Network or CAN protocol is a communication methodology between various electronic devices such as engine management systems, gear control, active suspension, ABS, lighting control, air conditioning, airbags, central locking, etc. For more learnings, check this article.

Here you will get an idea about programming PIC Microcontroller to interface with CAN Controller (MCP2515) to act as a transceiver. Here a PIC16f887 Microcontroller and an Arduino are used, one is to detect the temperature using LM35 and the other is to display the values ​​received through the CAN BUS.

Description:

Prerequisites and equipment:

You will need the following:

  1. A PIC16F877A and an Arduino board or Arduino clone (here's a guide if you need it)

  2. A 5v TTL-UART Bluetooth module.

  3. LCD and LM35 temperature sensor.

  4. Arduino IDE for programming.

  5. Two CAN Tranciever.

Working principle:

This project includes two distinct parts, transmitter section and a receiver section. The receiver section consists of an Arduino and an LCD to show the temperature reading received from the CAN bus. The transmitter section consists of an LM35 sensor interfaced with PIC16F877A. Communication between both sections is carried out by the MCP2515 CAN transceiver.

Temperature sensor CAN bus implementation:

The project block diagram is shown in Figure. The system consists of two CAN nodes. DISPLAY node that reads the temperature from the CAN bus and displays it on an LCD. This process is repeated continuously. The other node called the COLLECTOR node reads the temperature from an LM35 temperature sensor.

Visão geral da comunicação do microcontrolador Arduino e PIC pela interface CAN

Fig. 1: Overview of Arduino microcontroller and PIC communication via the CAN interface

The display processor:

The DISPLAY processor consists of an Arduino with an MCP2515 CAN module and a TJA1040 transceiver chip. The microcontroller is operated from a 16MHz crystal. And MCP2515 has an SPI interface which is used to connect using SPI pins on Arduino. The CANH and CANL pins of the transceiver chip are connected to the CAN bus. LCD is connected to Arduino to display temperature values.

Receiving CAN data and controlling relays:

Load the RX.ino program after saving it on your computer and open it in the Arduino IDE.

  • Compile the program in the Arduino IDE

The following function is used to receive values ​​from the CAN bus and display them on an LCD. Detailed instructions can be found here.

CAN.readMsgBuf(&len, buf);

Uploading software to Arduino:

If you are new to Arduino, you can start with here . You should start with the Arduino IDE (Arduino Integrated Development Environment) . Download the code from the link below and upload it to the Arduino board.

THE COLLECTOR processor:

The COLLECTOR processor consists of a PIC18F887 microcontroller with an MCP2515 CAN module and a TJA1040 transceiver chip. The PIC18F887 is operated from an 8 MHz crystal. The MCLR input is connected to an external reset button. The LM35DZ type semiconductor temperature sensor is connected to the analog input AN0 of the microcontroller. The sensor generates an analog voltage directly proportional to the measured temperature, the output is 10 mV/C. For example, at 20 degrees Celsius, the output voltage is 200 mV. The CANH and CANL outputs of this chip are connected directly to a braided cable that terminates on the CAN bus. The TJA1040 is an 8-pin chip that supports data rates of up to 1 Mb/s. The chip can power up to 112 nodes. A reference voltage equal to VDD/2 is output from pin 5 of the chip.

To change the speed of the MCP2515 Can Module on the collector side, you can use the calculator software provided by MikroC. Download the file included at the bottom to get the software.

Captura de tela dos cálculos CAN feitos para o microcontrolador MCP2515 no aplicativo MikroC

Fig. 2: Screenshot of CAN calculations made for the MCP2515 microcontroller in the MikroC application

This is the format to change the speed of the MCP2515 can module,

CANInitialize (SJW, BRP, Phase_Seg1, Phase_Seg2, Prop_Seg, init_flag);

Here is the initialization done here to reach 250Kbps

CANSPINIitialize (1,2,1,2,4,Can_Init_Flags);

For details on using MikroC to program pic controllers, see here.

The system works as follows:

  • The DISPLAY processor that waits for the current temperature message to be sent from the COLLECTOR processor via the CAN bus.

  • The COLLECTOR processor measures the temperature, formats it and sends it to the DISPLAY processor via the CAN bus.

  • The DISPLAY processor reads the message from the CAN bus and displays it on the LCD which is repeated every second.

Hardware assembly:

Imagem mostrando o circuito receptor CAN baseado em Arduino

Fig. 3: Image showing the Arduino-based CAN receiver circuit

Imagem mostrando o circuito transmissor CAN baseado em PIC MCP2515

Fig. 4: Image showing the CAN transmitter circuit based on MCP2515 PIC

Protótipo de circuitos transmissores e receptores CAN projetados em breadboards

Fig. 5: Prototype of CAN transmitter and receiver circuits designed on breadboards

Make the circuit as shown in the circuit diagram.

Project source code

Circuit diagrams

Circuit-Diagram-Arduino-PIC-Microcontroller-Based-CAN-Transmitter-Receiver-Circuits

Project video

Related Content

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.