Example Code for Arduino-Sensor Reading and Motor Control

Last revision 2025/12/10

The article provides example code for Arduino focusing on sensor reading and motor control, including steps for hardware and software preparation, wiring diagrams, and sample code for various sensors like accelerometers, gyroscopes, compasses, and pressure sensors. Readers will learn how to set up and initialize different components, capture data, and control motors using Arduino, making it an excellent resource for enthusiasts and developers looking to enhance their projects with sensor integration and motor functionality.

Hardware Preparation

Software Preparation

Sample Code

#include <stdio.h>
#include "wirish.h"
#include "i2c.h"

extern uint16 MotorData[6];  // Motor control commands

extern volatile unsigned int chan1PPM;  // PPM control command
extern volatile unsigned int chan2PPM;
extern volatile unsigned int chan3PPM;
extern volatile unsigned int chan4PPM;


char str[512];

////////////////////////////////////////////////////////////////////////////////////
// Function:  void setup()
// Parameter: None
// Return:    None
// Description: Initialization function
///////////////////////////////////////////////////////////////////////////////////
void setup()
{
  motorInit();       // Initialize motors
  capturePPMInit();  // Initialize PPM input signal capture function for remote control receiver

  // Configure I2C port 1 (pins 5, 9) with no special option flags (second argument)
  i2c_master_enable(I2C1, 0);  // Configure I2C1 interface, master mode

  initAcc();            // Initialize accelerometer
  initGyro();           // Initialize gyroscope
  bmp085Calibration();  // Initialize barometric altimeter
  compassInit(false);   // Initialize compass
  //compassCalibrate(1);  // Calibrate compass once with gain of 1.3Ga
  //compassSetMode(0);  // Set to continuous measurement mode

}
////////////////////////////////////////////////////////////////////////////////////
// Function prototype:  void loop()
// Parameter:          None
// Return:             None
// Description:        Main function, program main loop
///////////////////////////////////////////////////////////////////////////////////
void loop()
{
  //***************ADXL345 accelerometer reading test example*****************************
  int16 acc[3];
  getAccelerometerData(acc);  // Read accelerometer data
  SerialUSB.print("Xacc=");
  SerialUSB.print(acc[0]);
  SerialUSB.print("    ");
  SerialUSB.print("Yacc=");
  SerialUSB.print(acc[1]);
  SerialUSB.print("    ");
  SerialUSB.print("Zacc=");
  SerialUSB.print(acc[2]);
  SerialUSB.print("    ");
  //delay(100);
  //***********************************************************/


  //***************ITG3205 gyroscope reading test example*****************************
    int16 gyro[4];
    getGyroscopeData(gyro);    // Read gyroscope data

    SerialUSB.print("Xg=");
    SerialUSB.print(gyro[0]);
    SerialUSB.print("    ");
    SerialUSB.print("Yg=");
    SerialUSB.print(gyro[1]);
    SerialUSB.print("    ");
    SerialUSB.print("Zg=");
    SerialUSB.print(gyro[2]);
    SerialUSB.print("    ");
    //SerialUSB.print("temperature=");
    //SerialUSB.print(gyro[3]);
    //SerialUSB.print("    ");
    //delay(100);
  //*********************************************************************/
  //****************************BMP085 barometer test example******************
  int16 temperature = 0;
  int32 pressure = 0;
  int32 centimeters = 0;
  temperature = bmp085GetTemperature(bmp085ReadUT());
  pressure = bmp085GetPressure(bmp085ReadUP());
  centimeters = bmp085GetAltitude(); // Get altitude in centimeters
  //SerialUSB.print("Temperature: ");
  SerialUSB.print(temperature, DEC);
  SerialUSB.print(" *0.1 deg C ");
  //SerialUSB.print("Pressure: ");
  SerialUSB.print(pressure, DEC);
  SerialUSB.print(" Pa ");
  SerialUSB.print("Altitude: ");
  SerialUSB.print(centimeters, DEC);
   SerialUSB.print(" cm ");
  //SerialUSB.println("    ");
  //delay(1000);
  //********************************************************************/
  //******************************HMC5883 compass test****************************
  float Heading;
  Heading = compassHeading();
  //SerialUSB.print("compass: ");
  SerialUSB.print(Heading, DEC);
  SerialUSB.println(" degree");
  delay(100);
 //***************************************************************************/
  /*************************Motor drive test***************************************************************
  MotorData[0] = 1;  // First set PWM to minimum to activate ESC (Electronic Speed Controller)
  MotorData[1] = 1;
  MotorData[2] = 1;
  MotorData[3] = 1;
  MotorData[4] = 1;
  MotorData[5] = 1;
  motorCcontrol();   // Calculate the difference of each motor control value, use this value for timer interrupt 
                     // to change the high-level duration of corresponding motor pulses
  delay(1000);
  MotorData[0] = 500;  // Control 6 ESCs to run motors at half speed
  MotorData[1] = 500;
  MotorData[2] = 500;
  MotorData[3] = 500;
  MotorData[4] = 500;
  MotorData[5] = 500;
  motorCcontrol();   // Calculate the difference of each motor control value, use this value for timer interrupt
                     // to change the high-level duration of corresponding motor pulses
  while(1);
  *********************************************************************************************************/
  /********************RC remote controller PPM capture test****************************************************
  SerialUSB.print("PPM Channel 1: ");
  SerialUSB.print(chan1PPM, DEC);
  SerialUSB.print("  ");
  SerialUSB.print("PPM Channel 2: ");
  SerialUSB.print(chan2PPM, DEC);
  SerialUSB.print("  ");
  SerialUSB.print("PPM Channel 3: ");
  SerialUSB.print(chan3PPM, DEC);
  SerialUSB.print("  ");
  SerialUSB.print("PPM Channel 4: ");
  SerialUSB.print(chan4PPM, DEC);
  SerialUSB.println("  ");
  delay(100);
  ***************************************************************************************************/
}

Result

This sample code is a piece of the whole library code for the motor functions, you need to download the library to run the whole sketch which contains this motors code.

Was this article helpful?

TOP