Skip to main content

Duplex serial communication between Arduino and Raspberrypi

 Duplex serial communication between Arduino and Raspberrypi


Raspberrypi Setup 


Detect the Arduino board

When connecting the Arduino with a USB cable, you should see it appear as /dev/ttyACM0 or /dev/ttyUSB0 (sometimes the number can be different, for example /dev/ttyACM1).

Simply open terminal and run the following command. 

Run

ls /dev/tty*


and you should see it. At this point if you’re not sure which device is the Arduino board, simply disconnect the board (remove the USB cable), and run the above command again. This way you will easily spot the serial device name of your Arduino. For example in following pictures will observe the difference while running command before and after inserting arduino USB cable.


Before inserting USB


After inserting USB


Here the difference is there is added a port red marked.

Install Python Serial library on Raspberry Pi

You need to install a library to be able to use the Serial interface with Python.

To install it:

Run

python3 -m pip install pyserial

This Python library is well-known and used in a lot of applications.

When installing, if you get an error such as “/usr/bin/python3: No module named pip”, then you need to install pip first with 

Run

sudo apt install python3-pip

and Run

Sudo pip3 install pyserial.



Code for Raspberrypi


import serial                                         # Module needed for serial communication

import time

if __name__ == '__main__':

#change port instead ttyUSB0 by following above setup step.

    ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)

    ser.flush()

    while (1):

        send_string = ("My name is Raspberry Pi\n")

  # Send the string. Make sure you encode it before you send it to the Arduino.

        ser.write(send_string.encode('utf-8'))

  # Do nothing for 500 milliseconds (0.5 seconds)

        time.sleep(1)

  # Receive data from the Arduino

        receive_string = ser.readline().decode('utf-8').rstrip()

        print(receive_string)



Code for arduino


void setup() {

  Serial.begin(9600);

}


void loop() {

  if (Serial.available() > 0) {

    String data = Serial.readStringUntil('\n');

    Serial.print("You sent me: ");

    Serial.println(data);

  }

}


Comments

Popular posts from this blog

IOT based Weather monitoring system

  IOT based Weather monitoring system  Introduction In today's world, the importance of accurate and real-time weather monitoring cannot be overstated. Weather conditions significantly impact various aspects of life, from agriculture and transportation to disaster management and daily activities. With the advancement of technology, it is now possible to develop sophisticated yet affordable weather monitoring systems that can provide real-time data and alerts. One such innovative approach involves using the NodeMCU microcontroller and the DHT11 sensor. This project aims to design and implement a weather monitoring system that leverages these technologies to measure and report environmental parameters such as temperature and humidity. Objectives The primary objectives of this project are as follows: Design and Implementation: To design and implement a reliable weather monitoring system using the NodeMCU microcontroller and the DHT11 sensor. Real-time Data Acquisition: To acquire...

2017//2(b)//Engineering Eco//KU

A loan of $10,000 is to be repaid over a period of eight years. During the first four years, exactly half of the loan principal is to be repaid (along with accumulated compound interest) by a uniform series of payments of A1 dollar per year. The other half of the loan principal is to be repaid over four years, with accumulated interest, by a uniform series of payments of A2 dollar per year, If i=9% per year, what are A1 and A2?

2018//2(b)//Engineering Eco//KU

A large lithium-ion phosphate battery pack for an industrial application is expected to save $20,000 in annual energy expenses over its 6-year life. For a 3-year simple payback period, the permissible capital investment is $60,000. What is the internal rate of return on this $60,000 battery pack if it has a residual value of $10,000 at the end of 6 years? The MARR is 18% per year.