Skip to content

Arduino code examples for I2C, SPI, UART communication, stepper motor control, rotary encoder handling, and IMU integration.

License

Notifications You must be signed in to change notification settings

sahilrajpurkar03/ArduinoCode

Repository files navigation

Arduino Communication & Control Projects

A collection of standalone Arduino-based mini-projects demonstrating core embedded communication protocols (I2C, SPI, UART) and peripheral integrations (Stepper Motor, Rotary Encoder, IMU). Each project includes schematics, pin configurations, and tested Arduino code to help you learn and prototype quickly.

Installation

  1. Download and install the latest Arduino IDE from the official website:
    https://www.arduino.cc/en/software

  2. Install required libraries (if any) via Arduino Library Manager or GitHub:


Products & Official Links

Product Description Official Link
Arduino UNO/Mega Microcontroller Board Arduino UNO
MPU-9250 9-DoF IMU Sensor MPU-9250
Stepper Motor Stepper Motor Driver A4988 Stepper Drive
Rotary Encoder Incremental Rotary Encoder Rotary Encoder

Projects Overview

Each project contains:

  • Pinout diagram
  • Pin configuration
  • Code folder link

1. I2C Master Receive / Slave Transmit

I2C Pinout

This project demonstrates I2C communication where the Master requests data from the Slave, and the Slave transmits data back.

Pin Function Connection
A4 SDA (Data) Connect SDA pins of Master & Slave
A5 SCL (Clock) Connect SCL pins of Master & Slave
GND Ground Common Ground
5V Power Match board voltage

πŸ“‚ Code

Expected Behavior

  • The Master Arduino sends a request to the Slave for data.
  • The Slave Arduino responds by transmitting the requested data.
  • The Master receives and prints the data on its Serial Monitor.
  • Communication runs smoothly at 100 kHz (standard I2C speed).

2. I2C Master Transmit / Slave Receive

I2C Pinout

This project demonstrates I2C communication where the Master transmits data to the Slave, and the Slave receives it.

Pin Function Connection
A4 SDA (Data) Connect SDA pins of Master & Slave
A5 SCL (Clock) Connect SCL pins of Master & Slave
GND Ground Common Ground
5V Power Match board voltage

πŸ“‚ Code

Expected Behavior

  • The Master Arduino continuously sends data to the Slave.
  • The Slave Arduino receives the transmitted data and prints it on its Serial Monitor.
  • Data transmission occurs reliably without loss or errors.
  • Communication speed is set to standard I2C frequency.

3. SPI Communication – Master and Slave

SPI Pinout

This project demonstrates SPI communication between two Arduino boards:

  • One configured as the Master to transmit data.
  • One configured as the Slave to receive and display the data.

πŸ”Œ Pin Configuration (Common SPI Pins)

Pin Function Master Arduino Slave Arduino
11 MOSI (Master Out, Slave In) β†’ Connects to Slave's MOSI ← Receives from Master's MOSI
12 MISO (Master In, Slave Out) ← Connects to Slave's MISO β†’ Sends to Master's MISO
13 SCK (Clock) β†’ Drives Clock ← Receives Clock
10 SS (Slave Select) β†’ Pull LOW to select Input - Chip Select
GND Ground ↔ Common Ground ↔ Common Ground
5V Power (if needed) Shared if same board voltage Shared if same board voltage

⚠️ Connect MOSI to MOSI, MISO to MISO, SCK to SCK, and ensure common GND between devices.

πŸ“‚ Code

πŸ§ͺ Expected Behavior

  • The Master sends incrementing integer data over SPI every 2 seconds.
  • The Slave receives and prints the transmitted value via Serial Monitor.

4. UART Communication – Transmit and Receive Integer

UART Pinout

This project demonstrates UART communication between two Arduino boards, where one transmits integer data and the other receives and prints it.

πŸ”Œ Pin Configuration

Pin Function Transmitter Connection Receiver Connection
D1 TX β†’ RX of Receiver (D0)
D0 RX ← TX of Transmitter (D1)
GND Ground ↔ Common Ground ↔ Common Ground

⚠️ Make sure both Arduino boards share a common ground for UART to work correctly.

πŸ“‚ Code

πŸ§ͺ Expected Behavior

  • The transmitter continuously sends an increasing integer value every second.
  • The receiver reads the integer via UART and prints it to the serial monitor.

5. Stepper Motor Control

Stepper Pinout

This project controls a NEMA17 stepper motor using a simple step and direction interface.

The motor rotates to a specified angle in the clockwise direction and then reverses to the same angle in the counter-clockwise direction using digital pulses.

πŸ›  The code assumes a stepper driver (e.g., A4988 or DRV8825) connected to the Arduino.
Adjust steps per revolution in code if using a different motor or driver microstepping mode.

πŸ”Œ Pin Configuration

Pin Function Description
3 STEP Pulse to step motor
4 DIR Direction of rotation
GND Ground Common ground
5V Power Motor driver logic supply

πŸ“‚ Code

πŸ§ͺ Expected Behavior

  • On power-up, the motor:
    • Rotates 90 degrees clockwise
    • Waits for 2 seconds
    • Rotates 90 degrees counter-clockwise
    • Waits for 2 seconds
  • This cycle repeats continuously.

6. Rotary Encoder Integration

Encoder Pinout

This project reads the analog signal from a Pankaj Rotary Encoder (analog-type) to estimate the direction and amount of rotation.

⚠️ This is not a typical quadrature encoder. It outputs analog voltages which are used to determine relative movement.

πŸ”Œ Pin Configuration

Pin Function Description
A0 Analog output Reads voltage change from encoder
GND Ground Common ground
5V Power Sensor power supply

πŸ“‚ Code

πŸ§ͺ Expected Behavior

  • The code continuously monitors changes in analog voltage on A0.
  • It compares current and previous values to:
    • Increment a counter when turned clockwise
    • Decrement the counter when turned counter-clockwise
  • Output is printed to Serial Monitor as position updates (disp1).

7. IMU MPU9250 Integration

IMU Pinout

This project demonstrates integration of the SparkFun MPU9250 IMU sensor using the DMP (Digital Motion Processor) feature for real-time orientation tracking.

πŸ”Œ Pin Configuration

Pin Function Description
A4 SDA I2C Data Line
A5 SCL I2C Clock Line
3.3V Power Sensor power supply (3.3V)
GND Ground Common ground

πŸ“‚ Code

πŸ§ͺ Expected Behavior

  • The MPU9250 sensor initializes and starts its onboard DMP.
  • The sensor calculates quaternion data representing 3D orientation.
  • The Arduino reads and processes quaternion to compute yaw, pitch, and roll.
  • Yaw values are sent over Serial1.
  • The Serial Monitor displays debug info, and orientation data can be used for motion tracking applications.

Notes

  • Always ensure common ground between boards.
  • Match power voltage levels across devices.
  • Refer to respective folder for full code and comments.

Happy Coding!

About

Arduino code examples for I2C, SPI, UART communication, stepper motor control, rotary encoder handling, and IMU integration.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages