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:

PART NAME/DESCRIPTION VENDOR URL PRICE
(USD)
QTY
Raspberry Pi 5 Amazon.com https://shorturl.at/oXgrb 90.29 1
Arduino Amazon.com https://shorturl.at/Y5quT 27.60 1
Nema 17 Stepper Motor Amazon.com https://shorturl.at/4FKeo 9.99 1
AS5600 Magnetic Encoder (pack of 3) Amazon.com https://www.amazon.com/Magnetic-Encoder-Induction-Measurement-Precision/dp/B094F8H591/ref=sr_1_3?crid=19V1644J6E5AW&dib=eyJ2IjoiMSJ9.RhyFgSxBEhKQVC6-GmQeIsqHz0Hkgxb4JyHTwMPOu6YbPfblJcJwLOKi1uTXRHa26AECGaSbpIihDKOoQs3jcWgmyoIsk3D6X_3RTf9bHZ9ZX7GTnWWGjtRy5yPB9yFZwVeydzmLPB27d6pdo-a4yS4esmCVT54V-DUzmg2wCdCSiztStSk-b0PTNfvzF6Zye8GF1Iu-U4fibUYc_hf-dLrTC2iDidwznwTUkNERLCA.v4UnCOqLGgD7Pvg2SrW_5Ap8H7gjlkyxJuD038Eiq_w&dib_tag=se&keywords=as5600%2Bmagnetic%2Bencoder&qid=1718581726&sprefix=as5600%2Bmagnetic%2Bencoder%27%2Caps%2C156&sr=8-3&th=1 9.99 1
Arduino CNC Shield w/ 4 A4998 Motor Driver Amazon.com https://www.amazon.com/Organizer-Expansion-Stepper-Heatsink-Arduino/dp/B07TT3C3HB/ref=sr_1_4?crid=2L5ELGN72A2IJ&dib=eyJ2IjoiMSJ9.Nkdp_SFqaGCaUyYgMAgZzsH7_9YkehSI7bZrRA-1XZdaGVEtTF5D9_wEX0MrnTMU-M7yWbMwWjQIDE1uGoxeeQ7YAysvb_WH8o6xJ9WP9FSrpZOSr3CjbdX4_dYRPvSa8vS_by5ZALEAIWDnzNhSd_znVBtPvqzLjizCESlY5Enxomg8V3Tm5B_YdDuTB_seOTowz0JtOJeR8OqFLmmMbfXSBk2m_MlilLArO4DblOw.xOAPzYKjqeTMS8uXEtqgNA-4gMS7u0MPWUdgyaYVBiA&dib_tag=se&keywords=cnc+shield&qid=1718582693&sprefix=cnc+shiel%2Caps%2C172&sr=8-4 16.69 1
F-F Jumper Wires (pack of 40 and 10cm long) Amazon.com https://www.amazon.com/dp/B09FPL5QV8/ref=twister_B09G946VCF?_encoding=UTF8&th=1 5.99 1

Note:

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