Problem on using LCD and CAN-bus Shield together.

Hi everyone,

We are making a little project where we are using an UNO board and MCP2515 CAN-bus shield. We are measuring temperature with two DS18B20 sensors and measuring current with one sensor. Measurements should be sent into a LCD. Then those measurements should be sent into CAN-bus for further handling.

Everything (measurements, LCD) works fine until we wanna take CAN-bus on use. When we send a code including CAN.begin(CAN_250KBPS); line in it, LCD doesn't work anymore; it goes off and makes some kind blinking, but CAN-bus works fine.

So, what is the problem here that LCD doesn't work along with the CAN-bus? Pin collision? Out of memory/current? Serial connection?

#include <LiquidCrystal.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <mcp_can.h>
#include <SPI.h>
 
// LCD

// Connections:
// rs (LCD pin 4) to Arduino pin 12
// rw (LCD pin 5) to Arduino pin 11
// enable (LCD pin 6) to Arduino pin 10
// LCD pin 15 to Arduino pin 13
// LCD pins d4, d5, d6, d7 to Arduino pins 5, 4, 3, 2
LiquidCrystal lcd(12, 11, 9, 5, 4, 3, 2);
int backLight = 13;    // pin 13 will control the backlight

// temperature

// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 8
 
// Setup a oneWire instance to communicate with any OneWire devices 
// (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
 
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

// Sensor addresses
DeviceAddress T1 = { 0x28, 0xFF, 0xE5, 0xAC, 0x4C, 0x04, 0x00, 0x4A }; // pin 8
DeviceAddress T2 = { 0x28, 0xFF, 0x07, 0xC8, 0x4C, 0x04, 0x00, 0x8D }; // pin 8

// canbus
MCP_CAN CAN(10); //10
unsigned char stmp[1] = {123};

void setup()
{
  // LCD
  pinMode(backLight, OUTPUT);
  digitalWrite(backLight, HIGH); // turn backlight on. Replace 'HIGH' with 'LOW' to turn it off.
  lcd.begin(16,2);              // columns, rows.  use 16,2 for a 16x2 LCD, etc.
  lcd.clear();                  // start with a blank screen
  lcd.setCursor(0,0);           // set cursor to column 0, row 0 (the first row)
  
  // temperature
  // start serial port
  Serial.begin(9600);
  Serial.println("Dallas Temperature IC Control Library Demo");

  // Start up the library
  sensors.begin();

  // canbus
  CAN.begin(CAN_250KBPS);
  /*START_INIT:

    if(CAN_OK == CAN.begin(CAN_250KBPS))                   // init can bus : baudrate = 500k
    {
        Serial.println("CAN BUS Shield init ok!");
    }
    else
    {
        Serial.println("CAN BUS Shield init fail");
        Serial.println("Init CAN BUS Shield again");
        delay(100);
    //    goto START_INIT;
    }
  */
}

void loop()
{
  
  // temperature
  float Temp1 = sensors.getTempC(T1);
  float Temp2 = sensors.getTempC(T2);
  
  /*Serial.print(" Requesting temperatures...");
  sensors.requestTemperatures(); // Send the command to get temperatures
  Serial.println("DONE");

  Serial.print("Temperature for Device 1 is: ");
  Serial.print(Temp1);*/
  
  //LCD
  // T1
  lcd.setCursor(0,0);
  lcd.print("T1:");
  lcd.print(Temp1);
  lcd.setCursor(7,0);
  lcd.print(" ");
  
  // T2
  lcd.setCursor(8,0);
  lcd.print("T2:");
  lcd.print(Temp2);
  lcd.setCursor(15,0);
  lcd.print(" ");

  // current
  lcd.setCursor(0,1);
  lcd.print("I:");
  
  float average = 0;
  for(int i = 0; i < 1000; i++) {
    //average = average + (.0264 * analogRead(A0) -13.51) / 1000;
    //average = average + (0.0742 * analogRead(A0) -37.8787) / 1000;
    average = average + (0.0742 * analogRead(A0) -37.915) / 1000;
    delay(1);
  }
  Serial.println(average);
  
  lcd.print(average);
  
  // canbus
  CAN.sendMsgBuf(0x00, 0, 1, stmp);
  //delay(100);
  
}

Got it working. I'll tell the solution, if there is anyone with the same problem.

So, the problem was pin collision. I used pins 9-13 for the LCD, but UNO uses 10-13 pins for SPI, so needed to assign new pins for the LCD and keep those free.

1 Like

I stumbled across this after a couple of days of struggling (I'm new to this!)
Thanks for the fix and saving me a lot of additional frustration!

thanks for the big help.
i changed the lcd assigned Arduino pins from 11 & 12 to 7 & 8 then lcd works find under J1939.

1 Like