Arduino has a rich set of external interfaces, and the biggest difference from Raspberry Pi’s IO ports is that Arduino has analog input interfaces that can measure analog values on IO ports. The communication scheme between Arduino and Raspberry Pi via serial (Serial) generally has two methods: one is through Raspberry Pi GPIO serial communication, and the second is through USB serial communication.
Clearly, the USB serial communication between Arduino and Raspberry Pi is not only stable but also does not require complex cable connections. This article introduces how Raspberry Pi reads sensor values from Arduino via USB serial.
Connect the DHT11 sensor to Arduino to obtain the current temperature and humidity values, which are then sent out via serial.
1. Arduino Preparation
Download the required .zip library files for this project: dht11 (click on the end of the article 【Read the original】 to download)
#include
DHT dht;
#define DHT11_PIN 4
void setup(){
Serial.begin(9600);
Serial.println("DHT TEST PROGRAM ");
Serial.print("LIBRARY VERSION: ");
Serial.println(DHT11LIB_VERSION);
Serial.println();
Serial.println("Type,\tstatus,\tHumidity (%),\tTemperature (C)");
}
void loop(){
int chk;
Serial.print("DHT11, \t");
chk = dht.read(DHT11_PIN); // READ DATA
switch (chk){
case DHTLIB_OK:
Serial.print("OK,\t");
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.print("Checksum error,\t");
break;
case DHTLIB_ERROR_TIMEOUT:
Serial.print("Time out error,\t");
break;
default:
Serial.print("Unknown error,\t");
break;
}
// DISPLAY DATA
Serial.print(dht.humidity,1);
Serial.print(",\t");
Serial.println(dht.temperature,1);
delay(1000);
}
Compile and upload the program to Arduino, at this point you can obtain the data measured by the sensor from the Arduino serial.
2. Install Serial Debugging Program on Raspberry Pi
Minicom is a serial debugging tool for the Linux platform, equivalent to the serial debugging assistant on Windows, which can be used to read the sensor values sent by Arduino via USB serial. 1) Minicom Installation
sudo apt-get install minicom
2) Start Minicom
minicom -b 9600 -D /dev/ttyACM0
-b represents baud rate, -D represents port, /dev/ttyACM0 indicates opening the port connected to Arduino.
3. Connect Raspberry Pi and Arduino
Connect Arduino to Raspberry Pi via USB cable, at this point you can see the Arduino data obtained through the serial on the terminal of Raspberry Pi, which are the measured temperature and humidity values.
Reprinted from the Technology Enthusiast Blog
Leave a Comment
Your email address will not be published. Required fields are marked *