Skip to content

Electronica

Research and insparation

Motors

Motor choosing is an important part of the project, because the whole system must work correctly and reliably. In this part, I will talk about three motors between which I had to choose. These motors are: DC motor, stepper motor, and servo motor. I will describe the pluses and minuses of each of them.

Servo motor

Firstly, let’s talk about the servo motor. It is a very common and easy-to-use motor, and I have already worked with it before. The big minus for my project is that it is not strong enough and does not have enough torque to roll the threads and then come back. However, for first mini prototypes it is very useful, easy to control, and good for testing ideas.

DC motor

A DC motor is a good choice for robots or anything that needs strong, fast, and continuous movement. For my project, it is too fast. Another minus is that the angle is not controllable — you can control speed using voltage and change direction by reversing polarity, but this level of control is not enough for my needs. DC motors are cheap and work well. They are perfect for wheels, fans, pumps, and simple continuous motion.

Stepper motor

A stepper motor works with discrete steps instead of freely spinning. One step is usually 1.8°, which makes angle control very precise. It is excellent for repeatable motion. A stepper motor needs a driver (A4988, DRV8825, TMC, etc.). The minus is that it consumes power even when holding position. The big plus is that it can rotate continuously or move to exact positions, which makes it the best option for my project.

Comparison chart of motors

Parameter Servo Motor DC Motor Stepper Motor
Angle control Yes (limited range) No Yes (very precise)
Speed control Yes Yes Yes
Torque Low–Medium Medium–High High
Continuous rotation Limited / modified Yes Yes
Position accuracy Medium Very low Very high
External driver needed No No Yes
Power consumption when holding Low Low High
Best use cases Small mechanisms, prototypes Wheels, fans, pumps CNC, 3D printers, precise systems

After all this research and discussion with the Armenian lab staff, we decided to use a stepper motor for my final project, and a servo motor for the first experiments and examples. This motor will work with threads.

First coding steps

Ultrasonic sensor

#include <Adafruit_NeoPixel.h>

#define TRIG_PIN 10
#define ECHO_PIN 9
#define PIXEL_PIN 8   // Built-in NeoPixel on FLORA
#define NUMPIXELS 1

Adafruit_NeoPixel pixels(NUMPIXELS, PIXEL_PIN, NEO_GRB + NEO_KHZ800);

long duration;
float distance;

void setup() {
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);

  Serial.begin(9600);
  while (!Serial); // Needed for FLORA

  pixels.begin();
  pixels.clear();
  pixels.show();

  Serial.println("Ultrasonic sensor ready...");
}

void loop() {
  // Trigger ultrasonic pulse
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  // Read echo (long timeout for sensitivity)
  duration = pulseIn(ECHO_PIN, HIGH, 30000); // ~5 meters

  if (duration > 0) {
    distance = (duration * 0.0343) / 2;

    Serial.print("Distance: ");
    Serial.print(distance);
    Serial.println(" cm");

    // Very sensitive: anything detected
    if (distance > 2 && distance < 400) {
      pixels.setPixelColor(0, pixels.Color(255, 0, 0)); // RED
    } else {
      pixels.clear();
    }
  } else {
    // No echo detected
    Serial.println("No object detected");
    pixels.clear();
  }

  pixels.show();
  delay(80);
}

Ultrasonic sensor & servo motor

#include <Servo.h>

#define TRIG_PIN 10
#define ECHO_PIN 9
#define SERVO_PIN 6   // <-- Servo is on D6

Servo myServo;

long duration;
float distance;

int servoPos = 0;
bool directionUp = true;

void setup() {
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);

  Serial.begin(9600);
  while (!Serial); // Needed for FLORA

  myServo.attach(SERVO_PIN);
  myServo.write(0); // start position

  Serial.println("Ultrasonic + Servo ready...");
}

void loop() {
  // Trigger ultrasonic pulse
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  // Read echo
  duration = pulseIn(ECHO_PIN, HIGH, 30000);

  if (duration > 0) {
    distance = (duration * 0.0343) / 2;

    Serial.print("Distance: ");
    Serial.print(distance);
    Serial.println(" cm");

    // If something is detected
    if (distance > 2 && distance < 400) {

      // Move servo back and forth
      if (directionUp) {
        servoPos += 2;
        if (servoPos >= 90) directionUp = false;
      } else {
        servoPos -= 2;
        if (servoPos <= 0) directionUp = true;
      }

      myServo.write(servoPos);
    } else {
      // Nothing detected
      myServo.write(0);
    }

  } else {
    Serial.println("No object detected");
    myServo.write(0);
  }

  delay(30);
}