Table of Contents
Tutorial 2: Communication Between Arduino and Raspberry Pi
Author: Francis Palustre Email: palusf1@unlv.nevada.edu
Date: Last modified on <6/16/2024>
Keywords: Arduino, Raspberry Pi, Python, C++
Motivation and Audience
This tutorial's motivation is to learn how to apply bidirectional communication between a Raspberry Pi and Arduino to move a stepper motor with an AS5600 magnetic encoder attached.
Readers of this tutorial assumes the reader has the following background and interests:
* Know how to read and write Python and/or C++
* Know how to read and write using USB and I2C serial communication
* Know how to read and understand wiring diagrams
* This tutorial may also attract readers who are interested in creating motorized electronics that uses an Arduino and Raspberry Pi, or any similar devices
The rest of this tutorial is presented as follows and should be possible to gain a suitable understanding in a span of 1-2 hours:
Parts List and Sources
To complete this tutorial, you'll need the following items:
Note:
- You can replace the Raspberry Pi 5 with other models
- For this tutorial, one A4998 driver will be used, but all 4 will be used to create the vehicle
Communication Setup
To show the communication process, Python and C++ will be written for the Raspberry Pi to the Arduino. Although, for the examples below, Python will be used.
Raspberry Pi Code
#The following is the python version
//The following is the c++ version
Arduino Code
Wiring Diagram
The wiring diagram applies to all examples.
Beginner Example
The following code example is the Raspberry Pi telling the Arduino to rotate the stepper motor in 4 increments, while the encoder reads the current position of the motor.
Intermediate Example
The following code example is the Raspberry Pi telling the Arduino how many steps to rotate and how much it relates to the actual encoder value.
Advanced Example
The following code example is the Raspberry Pi telling the Arduino to revert back to its original position if the stepper motor's shaft has been moved.
Final Words