Arduino-Nextion display-MCP2515 CAN issue

Hi All
I am trying to build a digital display for my EV conversion project using a Nextion display. I am running with an Arduino Uno and a generic CAN to serial converter (MCP2515). I cannot get the CAN messages to display any values on the screen.

Here is what I have tested:

  • No issue with CAN bus with devices in car, 60 Ohm across H/L term, 500kB/s speed
  • Confirmed MCP2515 is functioning - serial port will display correct values (8MHz clock)
  • commented out #NexConfig Nextion file, confirmed Serial port
  • Use common ground between Nextion and Arduino, use TX (Arduino) - RX (Nextion) Yellow. One power supply/GND
  • Nextion display values correctly when I use directly with Arduino using analogue rotary pot input. Have tested another Nextion display, same issue.
#include <mcp_can.h>
#include <SPI.h>
#include "Nextion.h"

#define CS_PIN 10// varies between boards
#define INT_PIN 2 //varies between boards

MCP_CAN CAN0(CS_PIN);

long unsigned int rxId;
unsigned char len = 0;
unsigned char rxBuf[8];
int soc; // battery pack state of charge
//double packCurrent; // pack current, amps
//double packVoltage; // pack voltage, V
//double packPower; //pack power, kW


void setup() {
  
  Serial.begin(9600);  // Start serial comunication at baud=9600

  // I am going to change the Serial baud to a faster rate.
  delay(500);  // This delay is just in case the nextion display didn't start yet, to be sure it will receive the following command.
  Serial.print("bauds=115200");  // Set new baud rate of nextion to 115200, but it's temporal. Next time nextion is power on,
                                // it will retore to default baud of 9600.
                                // To take effect, make sure to reboot the arduino (reseting arduino is not enough).
                                // If you want to change the default baud, send the command as "bauds=115200", instead of "baud=115200".
                                // If you change the default baud, everytime the nextion is power ON is going to have that baud rate, and
                                // would not be necessery to set the baud on the setup anymore.
  Serial.write(0xff);  // We always have to send this three lines after each command sent to nextion.
  Serial.write(0xff);
  Serial.write(0xff);
  
  Serial.end();  // End the serial comunication of baud=9600
    
  Serial.begin(115200);
  
  if (CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_8MHZ) == CAN_OK)
    Serial.println("MCP2515 Initialised Successfully!");
  else
    Serial.println("MCP2515 Initialising Failed!");
  pinMode(INT_PIN, INPUT); // INT input pin

  CAN0.init_Mask(0, 0, 0x07FF0000); // Init mask to allow only 0x6B0 (hex) to pass
  CAN0.init_Filt(0, 0, 0x06B00000); // Init filter for 0x6B0 (hex)

  Serial.println("MCP2515 Library Mask & Filter Example...");
  CAN0.setMode(MCP_NORMAL); // Change to normal mode to allow messages to be transmitted
}

void loop() {
  if (!digitalRead(INT_PIN)) // If pin is low, read the receive buffer
  {
    CAN0.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, rxBuf = data byte(s)
    if (rxId == 0x6B0) { // Check if received ID is 0x6B0
      if (len >= 5) { // Ensure there are at least 5 bytes in the message
        soc = 0.5 * rxBuf[4]; // Store byte 4 as an integer in 'soc'.
        //packCurrent = 0.1* (((rxBuf[0] << 8) | rxBuf[1])); // -500 because we added 500 in Orion CANbus settings
        //packVoltage = 0.1 * ((rxBuf[2] << 8) | rxBuf[3]);
           
        //packPower = 0.001 * packVoltage * packCurrent;

      Serial.print("soc.val=");
      Serial.print(soc);
      Serial.write(0xff);  
      Serial.write(0xff);
      Serial.write(0xff);

      /*
        Serial.print("ID: ");
        Serial.print(rxId, HEX);
        Serial.print(" Data: ");
        for (int i = 0; i < len; i++) // Print each byte of the data
        {
          if (rxBuf[i] < 0x10) // If data byte is less than 0x10, add a leading zero
          {
            Serial.print("0");
          }
          Serial.print(rxBuf[i], HEX);
          Serial.print(" ");
        }
        Serial.print(" SOC: ");
        Serial.print(soc);

        Serial.print(" packCurrent: ");
        Serial.print(packCurrent);

        Serial.print(" Voltage: ");
        Serial.print(packVoltage);

        Serial.print(" packPowerkW: ");
        Serial.println(packPower);
      */
      
      }
    }
  }
}

The value I am attempting to display is state of charge (soc) - in serial monitor the HEX values are converted and are displaying the correct scaled value (82%) but nothing comes through to the display.
Any ideas what I should try next?

Hello @lorryoz ,
Welcome.

I have a tutorial on using Nextion displays here:

There's also another tutorial, which I have a link to in my tutorial, I suggest you read both and go with whichever methods you prefer. Neither uses the official Nextion library, which is not liked here. If you use my methods I'll try to help.

For get CAN for now, just get something printing on the Nextion successfully, then add the CAN stuff when you are confident you understand how to display stuff on the Nextion.

Do you know if that works? It takes ages to send data over serial, I am not sure what using Serial.end() does when there's still data waiting to be sent. I would think the first character has not left the buffer when you call end. As with getting something on the display before doing anything else keep the baud rate at the default 9600 until you know you can send stuff successfully. In any case, why are you not changing the baud rate on the Nextion in the configuration?

Using the same serial port as is used for the serial monitor can be made to work but it's asking for trouble. Ideally use a different Arduino board that has a spare hardware serial port, for example a Mega or a Nano Every. Failing that try software serial (but not at 115200 baud).

Hi Perry thanks for the reply. I have read your tutorials. I have both my nextion displays working when sending direct from arduino with GPIOs, turning pots etc. However as soon as i send over can it wont display. I have used 9600 baud and can't see why i doesn't work. I do have a Mega, maybe i should try that on Serial2 next?

Please explain more about how the Nextion is being powered.

Cheers.
I have updated the schematic above to include the power supply.

Using the Mega with the extra serial port is a good idea. You will be able to check the received data separately from sending it to the Nextion.

Alternatively, you can continue to use the uno as configured, but put the coms with the Nextion on software serial and use the serial monitor for checking.

2 Likes

Good idea cattledog I'll give it a go and report back

1 Like

To add to what @cattledog said:

You need to break the problem into small steps, or at least make things so that breaking into small steps is possible. With this in mind use the Mega, you can use Serial1, or Serial2 or Serial3 for the Nextion. Make sure you can send representative data to the Nextion.

Then, without involving the Nextion, read data from the MCP2525 (about which I know nothing) and send that data to the serial monitor. When you are happy that works OK then and only then try to send the MCP2525 data to the Nextion.

You might also find it useful to do 2 serial prints one after the other, the first being to the serial monitor and the second with exactly the same data to the Nextion, that way you can see what is being sent to the Nextion.

2 Likes

Yeah i thinks that's my problem. I throw it all together and just expect it to work. Ill get the Mega going and make a start. Cheers.

Well I got it working with the MEGA so thanks Perry and Cattledog for steering me in that direction.
For some reason with UNO didn't output to the Nextion display, despite others doing so. Weird
I moved to MEGA, changed the SPI pins, as I'm not using a shield:
SCK -> 52
MISO -> 50
MOSI -> 51
CS -> 53
I used Serial2 for the display, allowing me to check on Serial Monitor for testing.
Happy Days :slight_smile:

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.