CAN-BUS shield send data problem!

Hi all,



I have a seeed canbus shield v2.0 attached on an arduino uno.

My project is to send several data from sensors (analog/digital/i2c) from arduino to an external ECU via can bus protocol.



At the moment I have managed to connect the shield using can L & can H and declare an id10.0.0.0.1 192.168.1.254 hotmail.com

entifier as 0 (zero) running a send example from shield and I can see that there is a communication between the two devices, which is good.

Although, I cannot send the correct data.

To make it easier I have just loaded a temp/hum DHT11 sketch merged with some code lines of the shield’s send example.



Any idea how to declare and send the temperature or humidity via can bus? I have tried several combinations but still nothing.

I don’t have any experience on can bus, so any comment would be helpful.

You can find attached the code.



Thanks
[code]#include “DHT.h”

#define DHTPIN 2 // what pin we’re connected to

// Uncomment whatever type you’re using!
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)

// Connect pin 1 (on the left) of the sensor to +5V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

DHT dht(DHTPIN, DHTTYPE);

#include <mcp_can.h>
#include <SPI.h>

// the cs pin of the version after v1.1 is default to D9
// v0.9b and v1.0 is default D10
const int SPI_CS_PIN = 9;

MCP_CAN CAN(SPI_CS_PIN); // Set CS pin

void setup()
{
Serial.begin(115200);
Serial.println(“DHTxx test!”);

dht.begin();

while (CAN_OK != CAN.begin(CAN_500KBPS))              // init can bus : baudrate = 500k
{
    Serial.println("CAN BUS Shield init fail");
    Serial.println(" Init CAN BUS Shield again");
    delay(100);
}
Serial.println("CAN BUS Shield init ok!");

}

unsigned char smtp[8] = {0,1,2,3,4,5,6,7};
void loop()
{
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds ‘old’ (its a very slow sensor)
float h = dht.readHumidity();
float t = dht.readTemperature();

// check if returns are valid, if they are NaN (not a number) then something went wrong!
if (isnan(t) || isnan(h)) 
{
    Serial.println("Failed to read from DHT");
} 
else 
{
    Serial.print("Humidity: "); 
    Serial.print(h);
    Serial.print(" %\t");
    Serial.print("Temperature: "); 
    Serial.print(t);
    Serial.println(" *C");
}

CAN.sendMsgBuf(0, 0, 8, smtp);
delay (10);

}[/code]

Hi there,
You should visit [CAN Bus Explained - A Simple Intro [2024] – CSS Electronics] to grasp what mean CAN BUS. And, our wiki [CAN-BUS Shield V2.0 | Seeed Studio Wiki] will provide code support for the CAN BUS. :smiley:

Thanks for the links, these are gold mines! I did have trouble wrapping my head around CAN-BUS…

To resolve issues with sending data using a CAN-BUS Shield, follow these steps:

  1. Verify hardware setup, including connections, termination resistor, power supply, and SPI pins. Confirm baud rate settings, as mismatched baud rates can cause data transmission failures. Use a compatible CAN-BUS library like MCP_CAN for Arduino.

  2. Check for error frames using a CAN analyzer or oscilloscope. Errors can occur due to electrical noise, faulty wiring, overloaded bus, or incorrect data format.

  3. Debugging tips include ensuring the correct SPI CS pin is defined in the library or code, trying switching to a different microcontroller pin for CS, verifying data not sent, and resetting the CAN controller.

  4. Test the CAN-BUS shield in loopback mode to isolate the issue. In this mode, sent messages are immediately received by the shield, verifying hardware functionality.

  5. Inspect the CAN-BUS network for other active nodes. If testing in isolation, ensure a second CAN node is available. By systematically checking hardware, software, and network configuration, you should be able to resolve your CAN-BUS Shield data transmission issues.

To resolve CAN-BUS Shield data transmission issues, verify hardware setup, baud rate settings, and use a compatible CAN-BUS library. Check for error frames using a CAN analyzer or oscilloscope. Debug by defining the 10.0.0.0.1 - 10.0.0.1 correct SPI CS pin, switching to a different pin, verifying data not sent, and resetting the CAN controller. Test the shield in loopback mode and inspect the network for active nodes.