Skip to main content

Humidity_temperature (DHT11) using CAN MCP2515

Deep interesting in CAN BUS and working in this area encourage me to make a project using CAN BUS as a medium connection between two MCU. Today, i am gonna to share DHT11 temp sensor as a transmitter and give a output to serial monitor as Receiver using MCP2515.

Interesting right?:)





Code:

Transmitter :


#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 DHT 11 Pin Connected to Arduino
#define DHTTYPE DHT11 //DEFINE DHTTYPE

struct can_frame canMsg;
MCP2515 mcp2515(10);      //Set the pin number where SPI CS is connected (10 by default)

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 sensor value
 
  mcp2515.reset();
  mcp2515.setBitrate(CAN_500KBPS,MCP_8MHZ); //Sets CAN at speed 500KBPS and Clock 8MHz
  mcp2515.setNormalMode();
}

void loop()
{
  int h = 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] = t;               //Update temperature 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(1000);
}


Reciever:

#include <SPI.h>              //Library for using SPI Communication

#include <mcp2515.h>          //Library for using CAN Communication

struct can_frame canMsg;
MCP2515 mcp2515(10);                 // SPI CS Pin 10

void setup() {

  SPI.begin();                       //Begins SPI communication
  Serial.begin(9600);                //Begins Serial Communication at 9600 baudrate
 
  mcp2515.reset();                         
  mcp2515.setBitrate(CAN_500KBPS,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];
    
    Serial.print("Data from ID: 0x");
    Serial.print(canMsg.can_id, HEX);
    Serial.print(" ");
    Serial.print("DLC : ");
    Serial.print(canMsg.can_dlc);
    Serial.print(" ");
    Serial.print("DATA : ");
    Serial.print(canMsg.data[0]);
    Serial.print(" ");
    Serial.print(canMsg.data[1]);
    Serial.print(" ");
    Serial.print(canMsg.data[2]);
    Serial.print(" ");
    Serial.print(canMsg.data[3]);
    Serial.print(" ");
    Serial.print(canMsg.data[4]);
    Serial.print(" ");
    Serial.print(canMsg.data[5]);
    Serial.print(" ");
    Serial.print(canMsg.data[6]);
    Serial.print(" ");
    Serial.print(canMsg.data[7]);
    Serial.print("\n");
   
    Serial.print("Current humidity = ");   
    Serial.print(x);
    Serial.print("%  ");
    Serial.print("temperature = ");
    Serial.print(y);
    Serial.println("C  ");


    }
}

Output:


Comments

Popular posts from this blog

Arduino Interfacing With LCD(16x02) Without Potentiometer

All, today i gonna to share how Arduino connected to LCD(16x02). LCD 16x02 means 16 character in 2 rows. Connection between LCD and UNO Code: #include <LiquidCrystal.h> int Contrast=75;  LiquidCrystal lcd(12, 11, 5, 4, 3, 2);   void setup()  {     analogWrite(6,Contrast);      lcd.begin(16, 2);   }      void loop()  {      lcd.setCursor(0, 0);      lcd.print("ITS WORK");        lcd.setCursor(0, 1);      lcd.print("WELL DONE");  }

What You Need To Know About Battery In EV

What You Need To Know About Battery In EV Battery is used to supply power to your car. Battery have two type of terminal which is positive(anode) and negative(cathode). Generally, battery is combination single cell or multiple cell that used to generate or store energy. There are two type of battery which is primary and secondary. Primary is single used while secondary is rechargeable battery. A common primary battery is dry battery. Phone, smartwatch, others portable electronic devices and automobiles mostly used secondary battery.   Battery used in EV There have four kinds of battery using in EV car.  Lithium-ion Nickel-metal hydride Lead-acid Ultracapacitors Lithium-ion batteries Lithium-ion battery used in most electrical vehicle. It also using in our tablet, phone and other electronic devices. In 1991, Sony Corporation has produced and commercialized it first lithium-ion battery. Then, other company including Apples nowadays using it.  Takes from Apples when it ...

Ultrasonic Sensor With Arduino Uno

Yeah, Ultrasonic sensor now its working. Today i am gonna to share the code of how its work:) It quite easy. We just need to connect 4-pin which is VCC,GND,ECHO and TRG from Ultrasonic sensor to Arduino. Done:) Code : int trigger_pin = 4; int echo_pin = 2; int LED_pin = 13; int time; int distance; void setup ( ) {         Serial.begin (9600);         pinMode (trigger_pin, OUTPUT);         pinMode (echo_pin, INPUT);         pinMode (LED_pin, OUTPUT); } void loop ( ) {     digitalWrite (trigger_pin, HIGH);     delayMicroseconds (10);     digitalWrite (trigger_pin, LOW);     time = pulseIn (echo_pin, HIGH);     distance = (time * 0.034) / 2;   if (distance <= 10)         {  ...