Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino Nano Controlled Ultrasonic Distance Measurement with CAN Bus Interface and I2C LCD Display

View and Edit Project In Cirkit DesignerImage of Arduino Nano Controlled Ultrasonic Distance Measurement with CAN Bus Interface and I2C LCD Display

Circuit Documentation

Summary

The circuit in question is designed to interface an Arduino Nano with several peripheral devices, including an MCP2515 CAN controller, an LCD screen with I2C interface, and an HC-SR04 ultrasonic sensor. The Arduino Nano serves as the central processing unit, managing communication between the CAN network and the user interface provided by the LCD screen, as well as capturing distance measurements from the ultrasonic sensor. The circuit is powered by the Arduino Nano's 5V output, which is distributed to the other components.

Component List

MCP2515 CAN Controller

Arduino Nano

LCD Screen 16x2 I2C

HC-SR04 Ultrasonic Sensor

Wiring Details

MCP2515 CAN Controller

Arduino Nano

LCD Screen 16x2 I2C

HC-SR04 Ultrasonic Sensor

Documented Code

/**************I2C LCD display****************/
#include <LiquidCrystal_I2C.h>
 
LiquidCrystal_I2C lcd(0x20, 16, 2); // Format -> (Address,Width,Height )

/**********************CAN Communication************************************************/

#include <SPI.h>
#define SERIAL Serial
#define CAN_2515

#include "mcp2515_can.h"
const int SPI_CS_PIN = 10;
const int CAN_INT_PIN = 2;
mcp2515_can CAN(SPI_CS_PIN); // Set CS pin
unsigned char stmp[8] = {0, 0, 0, 0, 0, 0, 0, 0};

/**************HC-SR04 Ultrasonic Sensor****************/

#define trigPin 3 //attach pin D3 Arduino to pin Trig of HC-SR04
#define echoPin 4 // attach pin D4 Arduino to pin Echo of HC-SR04

// defines variables
long duration; // variable for the duration of sound wave travel
int distance; // variable for the distance measurement

void setup() {
    // Initialize I2C LCD display
    lcd.init();         // initialize the lcd
    lcd.backlight();    // Turn on the Backlight
    // Initialize ultrasonic sensor  
    pinMode(trigPin, OUTPUT);  // Sets the trigPin as an OUTPUT
    pinMode(echoPin, INPUT);    // Sets the echoPin as an INPUT
    Serial.begin(9600);        // Serial Communication is starting with 9600 of baud rate speed
    Serial.println("Ultrasonic Sensor HC-SR04 Test"); // print some text in Serial Monitor
    Serial.println("with Arduino NANO");
    // Initialize CAN communication
    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!");
    
}
void loop() {
    
    lcd.clear();            // Clear the display buffer
    lcd.setCursor(0, 0);    // Set cursor (Column, Row)        
    lcd.print("Response:");   // print "Response:" at (0, 0)
    lcd.setCursor(0,1);     // Set cursor (Column, Row)
    lcd.print("Distance:");    // print "Distance:" at (0, 1)
  

    digitalWrite(trigPin, LOW);     // Clears the trigPin condition
    delayMicroseconds(2);           // Sets the trigPin HIGH (ACTIVE) for 1000 microseconds
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(1000);
    digitalWrite(trigPin, LOW);
  
    duration = pulseIn(echoPin, HIGH);      // Reads the echoPin, returns the sound wave travel time in microseconds
                                            // Calculating the distance
    distance = duration * 0.034 / 2;        // Speed of sound wave divided by 2 (go and back)
                                            // Displays the distance on the Serial Monitor
    Serial.print("Distance: ");
    Serial.print(distance);
    Serial.println(" cm");                  
    lcd.setCursor(8, 0);    // Set cursor (Column, Row)        
    lcd.print(duration);   // print response time at (8, 0)
    lcd.setCursor(12,1);     // Set cursor (Column, Row)
    lcd.print(distance);    // print "Distance:" at (12, 1)


    // send data:  id = 0x00, standard frame, data len = 8, stmp: data buf
    stmp[7] = stmp[7] + 1;
    if (stmp[7] == 100) {
        stmp[7] = 0;
        stmp[6] = stmp[6] + 1;

        if (stmp[6] == 100) {
            stmp[6] = 0;
            stmp[5] = stmp[6] + 1;
        }
    }

    CAN.sendMsgBuf(0x00, 0, 8, stmp);
    delay(100);                       // send data per 100ms
    SERIAL.println("CAN BUS sendMsgBuf ok!");
}

This code is responsible for initializing and managing the LCD display, ultrasonic sensor, and CAN communication. It includes setup routines for each component and a loop that continuously measures distance, updates the LCD display, and sends data over the CAN bus.