this code follow the flow chart : ultrasound senso...
Erstellt am: 28. Februar 2025
Erstellt am: 28. Februar 2025
this code follow the flow chart :
ultrasound sensor measures the displacement - the Arduino displays the displacement value; the rotation of the stepper motor is mapped based on the displacement value data from the ultrasound sensor- following the image table shared- and shows the degree rotation given to the motor each time. Also, displacement measured by the sensor is mapped with the pinion rotation for every 10mm difference in the displacement from its previous value.
if the difference is negative the direction of rotation be clockwise and for it being positive the direction of rotation be anticlockwise. generate the Arduino code.
#include <Stepper.h>
#define TRIG_PIN 9
#define ECHO_PIN 10
#define STEPS_PER_REV 200
Stepper stepperMotor(STEPS_PER_REV, 8, 9, 10, 11);
float previousDisplacement = 0.0;
float displacement = 0.0;
float rotationAngle = 0.0;
float mapDisplacementToRotation(float disp) {
if (disp >= 0 && disp <= 100) {
return (189.0762 / 100.0) * disp;
}
return 0.0;
}
float measureDisplacement() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
textlong duration = pulseIn(ECHO_PIN, HIGH); float distance = duration * 0.034 / 2; return distance;
}
void setup() {
Serial.begin(9600);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
stepperMotor.setSpeed(60);
}
void loop() {
displacement = measureDisplacement();
float mappedRotation = mapDisplacementToRotation(displacement);
float rotationDifference = mappedRotation - rotationAngle;
textif (abs(displacement - previousDisplacement) >= 10.0) { int stepsToMove = (rotationDifference / 360.0) * STEPS_PER_REV; if (rotationDifference > 0) { stepperMotor.step(stepsToMove); // Anticlockwise } else { stepperMotor.step(-stepsToMove); // Clockwise } Serial.print("Displacement: "); Serial.print(displacement); Serial.print(" mm, Pinion Rotation: "); Serial.print(mappedRotation); Serial.println(" degrees"); previousDisplacement = displacement; rotationAngle = mappedRotation; } delay(1000);
}
listen this is the code which is rotate only in one direction that is clockwise even if the difference from the negative and the current reading is negative, the motor is rotating in the same direction as before when the difference positive please change the code so that is rotates in both the direction clockwise and anti-clock wise please
Below is an updated version of your code that separates the ultrasonic pins from the stepper‐motor pins (so they do not conflict) and ensures the motor can run in both directions. A common pitfall was that TRIG_PIN and ECHO_PIN were using the same pins as the stepper motor. Changing the ultrasonic pins to something else (e.g. 2 and 3) typically fixes the “only one‐direction” issue. The rest of the direction logic (negative rotation difference → clockwise, positive → anticlockwise) remains the same.
cpp#include <Stepper.h> // --- Pin definitions --- #define TRIG_PIN 2 #define ECHO_PIN 3 // Stepper motor pins (must be different from TRIG/ECHO): #define STEPPER_PIN_1 8 #define STEPPER_PIN_2 9 #define STEPPER_PIN_3 10 #define STEPPER_PIN_4 11 // --- Stepper config --- #define STEPS_PER_REV 200 Stepper stepperMotor(STEPS_PER_REV, STEPPER_PIN_1, STEPPER_PIN_2, STEPPER_PIN_3, STEPPER_PIN_4); // --- Variables --- float previousDisplacement = 0.0; float displacement = 0.0; float rotationAngle = 0.0; // Our “current” rotation in degrees // --- Map displacement 0..100 mm -> 0..~189 deg --- float mapDisplacementToRotation(float disp) { if (disp >= 0 && disp <= 100) { return (189.0762f / 100.0f) * disp; } return 0.0; } // --- Measure distance via ultrasonic sensor --- float measureDisplacement() { digitalWrite(TRIG_PIN, LOW); delayMicroseconds(2); digitalWrite(TRIG_PIN, HIGH); delayMicroseconds(10); digitalWrite(TRIG_PIN, LOW); long duration = pulseIn(ECHO_PIN, HIGH); // Calculate distance in mm (duration * speed of sound / 2) float distance = duration * 0.034f / 2.0f; return distance; } void setup() { Serial.begin(9600); // Initialize ultrasonic pins pinMode(TRIG_PIN, OUTPUT); pinMode(ECHO_PIN, INPUT); // Set stepper speed (RPM) stepperMotor.setSpeed(60); } void loop() { // Read current displacement from sensor displacement = measureDisplacement(); // Map displacement to desired pinion rotation angle float mappedRotation = mapDisplacementToRotation(displacement); // How much rotation is needed since last time float rotationDifference = mappedRotation - rotationAngle; // Only move if we've changed by ≥ 10 mm since last move if (fabs(displacement - previousDisplacement) >= 10.0) { // Convert degrees → steps (the sign will set direction) int stepsToMove = int( (rotationDifference / 360.0f) * STEPS_PER_REV ); if (rotationDifference > 0) { // Positive difference → anticlockwise stepperMotor.step(stepsToMove); } else { // Negative difference → clockwise stepperMotor.step(stepsToMove); // stepsToMove will be negative here } // Print info Serial.print("Displacement: "); Serial.print(displacement); Serial.print(" mm, Pinion Rotation: "); Serial.print(mappedRotation); Serial.println(" degrees"); // Update our “previous” markers previousDisplacement = displacement; rotationAngle = mappedRotation; } // Slow down loop a bit delay(1000); }
Separate the ultrasonic pins from stepper pins:
TRIG_PIN = 2
, ECHO_PIN = 3
8, 9, 10, 11
.Direction logic:
rotationDifference
is positive, we call stepperMotor.step(...)
with a positive step count (anticlockwise).rotationDifference
is negative, the computed steps are negative, so calling stepperMotor.step(stepsToMove)
will make the motor rotate clockwise.With these fixes, you should see correct forward (anticlockwise) and reverse (clockwise) motion based on whether the new displacement is larger or smaller than the old displacement.
give the connections of all the pins stepper motor with driver TB6560 aurdino uno and ultrasonic sensor
Below is a typical way to wire an Arduino Uno, a TB6560‐based stepper driver, a four‐wire bipolar stepper motor, and your ultrasonic sensor. Keep in mind that the simple four‐wire “Stepper.h” code you posted was originally meant to drive the motor coils directly from four Arduino pins. The TB6560, however, is a separate step‐and‐direction driver and usually expects just two or three control signals (STEP, DIR, and possibly ENABLE).
A four‐wire bipolar stepper has two coils:
Make sure you know which pair of wires from your stepper motor belongs to the same coil. A quick way: measure continuity or resistance; each pair with non‐infinite resistance is one coil.
textStepper Motor ----> TB6560 Coil1 Wire1 ---> A+ Coil1 Wire2 ---> A- Coil2 Wire1 ---> B+ Coil2 Wire2 ---> B-
A typical TB6560 has STEP, DIR, and EN (enable) inputs. It often provides each signal in “+” and “–” terminals for use with an internal optocoupler. One common approach is to tie the “–” pin to Arduino GND and feed the “+” pin from an Arduino digital output (through a current‐limiting resistor if needed). Another approach is to tie the “+” pins to +5 V and pull the “–” pins to ground with the Arduino pins. The exact hookup depends on your particular TB6560 board’s labeling.
A common pin assignment (assuming 5 V logic) is:
Meanwhile, your TB6560 board will need its own motor power supply (often 12 V to 24 V) connected to the board’s V+ and GND power terminals.
Important: If you want to use the “Stepper.h” library as is, you normally do not go through a step/dir driver. You would drive the 4 motor wires directly from 4 Arduino pins through an H‐bridge or transistor arrangement. To make use of the TB6560’s step/dir interface, you should switch to a different library (e.g. AccelStepper in “Driver” mode) or write your own code that toggles STEP and sets DIR accordingly.
Below is one way to assign the TB6560 pins if you adapt your code to produce a single STEP pulse and a DIR signal:
textTB6560 Arduino ----- ------- Pul+ (Step+) - D8 Pul- (Step-) - GND Dir+ - D9 Dir- - GND EN+ - +5 V (if you want always enabled) EN- - GND GND (Board) - Arduino GND V+ (12-24 V) - External motor power supply
The code you posted uses:
cpp#define TRIG_PIN 2 #define ECHO_PIN 3
So wire as follows:
All grounds (Arduino GND, TB6560 driver GND, ultrasonic sensor GND, and power‐supply negative) should be connected together so that signal references match.
Your posted code uses:
cppStepper stepperMotor(STEPS_PER_REV, 8, 9, 10, 11);
That is the 4‐pin drive style of the built‐in Stepper library. The TB6560 replaces that 4‐pin “H‐bridge” logic with its own step/dir driver. If you actually want to use the TB6560, you typically do not use the Stepper
library in that 4‐wire way. Instead, you would:
AccelStepper
in “Driver” mode).For example, using AccelStepper:
cpp#include <AccelStepper.h> #define PIN_STEP 8 #define PIN_DIR 9 AccelStepper stepper(AccelStepper::DRIVER, PIN_STEP, PIN_DIR); void setup() { stepper.setMaxSpeed(200); stepper.setAcceleration(100); } void loop() { // Example: rotate forward stepper.moveTo(200); while(stepper.distanceToGo() != 0) { stepper.run(); } // Then rotate back stepper.moveTo(-200); while(stepper.distanceToGo() != 0) { stepper.run(); } }
This approach is the usual way to interface with a TB6560 or similar step/dir driver.
In short:
That way, you get correct clockwise/anticlockwise rotation from the TB6560 and can still combine it with your ultrasonic measurements.