Cirkit Designer Logo
Cirkit Designer
Your all-in-one circuit design IDE
Home / 
Project Documentation

Arduino-Controlled Stepper Motor with Sound Sensing and Variable Speed

Image of Arduino-Controlled Stepper Motor with Sound Sensing and Variable Speed

Circuit Documentation

Summary

This circuit is designed to control a gear-reduced stepper motor based on inputs from a sound sensor and a potentiometer. The Arduino UNO serves as the central microcontroller unit, processing the sensor data and controlling the motor's speed and direction. The sound sensor detects ambient noise levels, while the potentiometer allows for manual control of the motor's speed. The stepper motor's speed is dynamically adjusted based on the sound sensor's input and the potentiometer's position.

Component List

Adafruit Gear-Reduced Stepper Motor (small)

  • Description: A small stepper motor with gear reduction for increased torque.
  • Pins: RED, ORANGE, YELLOW, PINK, BLUE

Sound Sensor

  • Description: A sensor that detects the intensity of sound in the environment.
  • Pins: 5V +, OUT, GND

Potentiometer

  • Description: A variable resistor that provides an adjustable voltage output.
  • Pins: GND, Output, VCC

Arduino UNO

  • Description: A microcontroller board based on the ATmega328P.
  • Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0, A1, A2, A3, A4, A5, SCL, SDA, AREF, D13, D12, D11, D10, D9, D8, D7, D6, D5, D4, D3, D2, D1, D0

Wiring Details

Adafruit Gear-Reduced Stepper Motor (small)

  • RED connected to Arduino UNO GND
  • ORANGE connected to Arduino UNO D11
  • YELLOW connected to Arduino UNO D10
  • PINK connected to Arduino UNO D9
  • BLUE connected to Arduino UNO D8

Sound Sensor

  • 5V + connected to Arduino UNO 5V
  • OUT connected to Arduino UNO A1
  • GND connected to Arduino UNO GND

Potentiometer

  • VCC connected to Arduino UNO 5V
  • Output connected to Arduino UNO A0
  • GND connected to Arduino UNO GND

Documented Code

#include <Stepper.h>
#include <Wire.h>

// Define the pins
const int soundSensorPin = A0; // Sound sensor connected to A0
const int potPin = A1;          // Potentiometer connected to A1

const int stepsPerRevolution = 200; 
// Change this to fit the number of steps per revolution for your motor

// Initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

float receivedFloat = 0;  // For temperature
float riskFactor = 0;
const float decayRate = 0.95; // Decay rate for risk factor
const float maxSpeed = 90; // Max speed for the stepper motor
const float minSpeed = 20; // Minimum speed for the stepper motor

void setup()
{
  Serial.begin(9600);
  // Set the initial speed:
  myStepper.setSpeed(maxSpeed);
  Wire.begin(8);  // Set the I2C address of this Arduino as 8
  Wire.onReceive(receiveEvent);  // Setup to handle incoming data
}

void loop()
{
  // Rotate the stepper motor anticlockwise
  myStepper.step(-stepsPerRevolution); 

  // Read the analog value from the sound sensor
  int soundValue = analogRead(soundSensorPin);
  // Read the potentiometer value
  int potValue = analogRead(potPin); // Read the analog value from the potentiometer
  // Compute risk factor based on sound and temperature
  riskFactor = computeRiskFactor(soundValue, receivedFloat);

  // Adjust stepper speed based on the risk factor and potentiometer
  float mappedSpeed = map(potValue, 0, 1023, minSpeed, maxSpeed);
  // If the risk factor is high, reduce the speed
  if (riskFactor > 50) {
    mappedSpeed = min(mappedSpeed, 60); // Limit to 60 if risk factor is high
  }
  myStepper.setSpeed(mappedSpeed);

  // Print values for plotting
  Serial.print("Sound Value: ");
  Serial.println(soundValue);  // Print the sound value
  Serial.print("Temperature: ");
  Serial.println(receivedFloat); // Print the received temperature
  Serial.print("Potentiometer Value: ");
  Serial.println(potValue);  // Print the potentiometer value
  Serial.print("Mapped Speed: ");
  Serial.println(mappedSpeed); // Print the mapped speed
  Serial.print("Risk Factor: ");
  Serial.println(riskFactor); // Print the risk factor

  // Apply decay to risk factor if it hasn't been updated
  riskFactor *= decayRate;
}

// Function to handle incoming data
void receiveEvent(int howMany)
{
  byte byteArray[sizeof(float)];
  int i = 0;
  while (Wire.available() && i < sizeof(float))
  {
    byteArray[i] = Wire.read();  // Read each byte
    i++;
  }

  // Convert byte array back to float
  memcpy(&receivedFloat, byteArray, sizeof(float));
}

// Function to compute risk factor
float computeRiskFactor(int soundValue, float temperature)
{
  // Normalize values
  float normalizedSound = map(soundValue, 0, 1023, 0, 100); // Assuming 0-1023 range for sound
  float normalizedTemp = map(temperature, 0, 100, 0, 100); // Assuming 0-100°C for temperature

  // Increase weight for sound
  float weightedSound = normalizedSound * 0.9; // Weight for sound
  float weightedTemp = normalizedTemp * 0.1;   // Weight for temperature

  // Calculate risk factor
  return weightedSound + weightedTemp; // Combine factors
}

This code is designed to run on an Arduino UNO and controls a stepper motor based on the input from a sound sensor and a potentiometer. The setup() function initializes the serial communication, stepper motor, and I2C communication. The loop() function reads the sensor values, computes a risk factor, and adjusts the motor speed accordingly. The receiveEvent() function handles incoming I2C data, and the computeRiskFactor() function calculates a risk factor based on sound and temperature.