Hello,
I'm trying to read Value from potentiometer using Arduino DUE, send it via CAN to Raspberry Pi Pico and show it on LCD. But it doesn't work and I have problem with troubleshooting. I don't know if the problem is between DUE and Can module or second Can module and Pico.
It is connected in following order:
I am using Arduino IDE and there is my code for both devices (there are no errors):
//CAN Transmitter Code (Arduino DUE):
#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
struct can_frame canMsg;
MCP2515 mcp2515(52);
int potPin = 0;
int val = 200;
//DHT dht(DHTPIN, 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
mcp2515.reset();
mcp2515.setBitrate(CAN_250KBPS,MCP_8MHZ); //Sets CAN at speed 500KBPS and Clock 8MHz
mcp2515.setNormalMode();
}
void loop()
{
val = analogRead(potPin);
int h=7; //= 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] = h; //Update humidity value in [0]
canMsg.data[1] = val; //Update potentiometer value in [1]
canMsg.data[2] = 0x00; //Rest all with 0
canMsg.data[3] = 0x00;
canMsg.data[4] = 0x00;
canMsg.data[5] = 0x00;
canMsg.data[6] = 0x00;
canMsg.data[7] = 0x00;
mcp2515.sendMessage(&canMsg); //Sends the CAN message
delay(100);
}
//CAN Receiver Code (Pico):
#include <SPI.h> //Library for using SPI Communication
#include <Wire.h> // Include Wire Library for I2C
#include <mcp2515.h> //Library for using CAN Communication
//#include <LiquidCrystal.h> //Library for using LCD display
#include <LiquidCrystal_I2C.h> // Include NewLiquidCrystal Library for I2C
// Define LCD pinout
const int en = 2, rw = 1, rs = 0, d4 = 4, d5 = 5, d6 = 6, d7 = 7, bl = 3;
// Define I2C Address - change if reqiuired
const int i2c_addr = 0x27;
LiquidCrystal_I2C lcd(i2c_addr, en, rw, rs, d4, d5, d6, d7, bl, POSITIVE);
struct can_frame canMsg;
MCP2515 mcp2515(5); // SPI CS Pin 10
void setup() {
lcd.begin(16,2); //Sets LCD as 16x2 type
lcd.setCursor(0,0); //Display Welcome Message
lcd.print("CIRCUIT DIGEST");
lcd.setCursor(0,1);
lcd.print("CAN ARDUINO");
delay(1000);
lcd.clear();
SPI.begin(); //Begins SPI communication
Serial.begin(9600); //Begins Serial Communication at 9600 baudrate
mcp2515.reset();
mcp2515.setBitrate(CAN_250KBPS,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[0];
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,1);
lcd.print("Temp : ");
lcd.print(y);
delay(100);
lcd.clear();
}
}
Everything is based on this tutorial, and that is why you can see temperature and humidity in code, but i changed variables:
Reading value from potentiometer, and connection between Pico and LCD are ok.
I think i messed something up with SPI.