13-03-2024, 23:08
I am bulding an arduino based robot. The robot is supposed to drive away from the light. I'm using:
There is no problem with photoresistors or code connected to calculating motorspeed, it is printed on serial monitor correctly, but the wheels won't spin, the motors make the noise typical for PWM. Is there something wrong with the voltage or something? Should I use different batteries? Maybe use resistors with h-bridge? I tried powering the whole project with just 9V battery connected to arduino connected to breadboard with h-bridge, it didn't work too. Any ideas what I am doing wrong?
- dc motors:
- Motor supply voltage: max. 6 V
- Torque: 0.8 kg*cm (0.78 Nm)
- Engine speed without load: 90 +/- 10 rpm
- Motor current consumption without load: 190 mA (max. 250 mA)
- Motor current consumption when the shaft is stopped: 1 A
- h-bridge - L293D
These are two-channel ICs, designed for use with voltages up to 36 V and current not exceeding 0.6 A per channel (temporary current can reach a maximum of 1.2 A per channel).
Connected like this:
- 2 Photoresistors 5-10kΩ GL5616
- 2 10k resistors (to photoresistors)
- 6V battery connected to h-bridge
- Arduino is powered by laptop but it will be powered by 9V battery in the final project
My code looks like this:
Kod:
// Definicje pinów czujników światła
const int leftSensorPin = A0; // Pin dla lewego czujnika światła
const int rightSensorPin = A1; // Pin dla prawego czujnika światła
// Definicje pinów sterujących silnikami
const int leftMotorPin1 = 4; // Pin 1 dla lewego silnika
const int leftMotorPin2 = 5; // Pin 2 dla lewego silnika
const int leftMotorPinSteering = 6;
const int rightMotorPin1 = 8; // Pin 1 dla prawego silnika
const int rightMotorPin2 = 7; // Pin 2 dla prawego silnika
const int rightMotorPinSteering = 9;
// Stałe prędkości silników
const int baseSpeed = 100; // Bazowa prędkość silników
void setup() {
// Inicjalizacja pinów jako wyjścia
pinMode(leftSensorPin, INPUT);
pinMode(rightSensorPin, INPUT);
pinMode(leftMotorPin1, OUTPUT);
pinMode(leftMotorPin2, OUTPUT);
pinMode(rightMotorPin1, OUTPUT);
pinMode(rightMotorPin2, OUTPUT);
pinMode(leftMotorPinSteering, OUTPUT);
pinMode(rightMotorPinSteering, OUTPUT);
Serial.begin(9600);
}
void loop() {
// Odczyt wartości z czujników światła
int leftLight = analogRead(leftSensorPin);
int rightLight = analogRead(rightSensorPin);
// Obliczenie różnicy między wartościami czujników
int lightDifference = leftLight - rightLight;
// Obliczenie prędkości silników w zależności od różnicy wartości odczytów
int leftSpeed = baseSpeed + lightDifference;
int rightSpeed = baseSpeed - lightDifference;
// Ograniczenie prędkości do zakresu 0-255
leftSpeed = constrain(leftSpeed, 0, 200);
rightSpeed = constrain(rightSpeed, 0, 200);
Serial.print("Left Light: ");
Serial.print(leftLight);
Serial.print(" | Right Light: ");
Serial.println(rightLight);
Serial.print("Left speed: ");
Serial.print(leftSpeed);
Serial.print(" | RrightSpeed: ");
Serial.println(rightSpeed);
delay(10000);
// Ustawienie kierunku obrotów silników
digitalWrite(leftMotorPin1, HIGH);
digitalWrite(leftMotorPin2, LOW);
digitalWrite(rightMotorPin1, HIGH);
digitalWrite(rightMotorPin2, LOW);
analogWrite(leftMotorPinSteering, leftSpeed);
analogWrite(rightMotorPinSteering, rightSpeed);
}
There is no problem with photoresistors or code connected to calculating motorspeed, it is printed on serial monitor correctly, but the wheels won't spin, the motors make the noise typical for PWM. Is there something wrong with the voltage or something? Should I use different batteries? Maybe use resistors with h-bridge? I tried powering the whole project with just 9V battery connected to arduino connected to breadboard with h-bridge, it didn't work too. Any ideas what I am doing wrong?