Hi,
I am building a water pump to run at varying speeds based on data in an SD card. The data
is time and speed. I am using an Arduino Uno. I have been struggling with getting the
correct values put into the Arduino to relay to a L298N motor driver. The input values are
anywhere from 0-50 which I hope to map to Arduino’s 0-255 PWM using map function. The
output I am receiving is in the 1000s and comes in negative values too but the inputs are
positive values. Additionally, please note the data in the SD card is in decimal format which
I am hoping to keep. Below is a copy of the code I have been modifying and working with to
try to accomplish this as I hope someone can help me tweak it : #include <SD.h> #include <Servo.h> #include <Ethernet.h>
//const int pumpPin = 9; // PWM-enabled pin connected to the pump
const int relayPin = A4; // Pin connected to the IN pin of the relay
const int sdChipSelect = 4; // CS pin for the SD card module
const int ethernetChipSelect = 10; // CS pin for the Ethernet module
Servo pumpControl;
File file;
byte mac[] = { 0xA8, 0x61, 0x0A, 0xAE, 0xF0, 0x72 }; // Replace with your Ethernet shield's
MAC address
void setup() {
Serial.begin(500000);
pumpControl.attach(relayPin); // Attach the servo to the specified pin
//pinMode(pumpPin, OUTPUT);
pinMode(relayPin, OUTPUT);
// Initialize Ethernet and SD card
// if (Ethernet.begin(mac) == 0) {
// Serial.println("Failed to configure Ethernet using DHCP");
// while (1);
// }
// Initialize SD card
if (!SD.begin(sdChipSelect)) {
Serial.println("SD card initialization failed!");
while (1);
}
// Open the file
file = SD.open("ttt.txt");
if (file) {
Serial.println("File opened successfully");
} else {
Serial.println("Error opening file");
while (1);
}
}
void loop() {
// Read speed from file
if (file.available()) {
unsigned long speed = file.parseInt(); // Read speed value from the file
setPumpSpeed(speed);
Serial.print("Speed set to: ");
Serial.println(speed);
// // Turn on the pump using the relay
// digitalWrite(relayPin, HIGH);
// delay(1000); // Pump runs for 5 seconds
//
// // Turn off the pump using the relay
// digitalWrite(relayPin, LOW);
// delay(1000); // Pump is off for 5 seconds
} else {
// End of file reached, close and reopen the file
file.close();
file = SD.open("ttt.txt");
}
//delay(1000); // Adjust the delay as needed
}
void setPumpSpeed(int speed) {
// Map the speed value to a PWM range (0-255)
int pwmValue = map(speed, 0, 1023, 0, 255);
analogWrite(relayPin, pwmValue);
}
Thank you!
Welcome! Before I try to read your code and figure out what you want please spend the time to read the forum guidelines which apparently you did not and post your code using code tags. Since I cannot see what you have also post links to technical information on the hardware items and your preliminary schematic be sure to show all power, ground and power sources. Useful links - check here for reference posts / tutorials Frizzing diagrams are not considered schematics here.
Here are the schematics with technology links, output values, input text file, and code (in order). The serial monitor outputs do not match that of our input from the SD card. We are trying to figure out how to fix this so that our L298N motor driver will run the water pump at variable speeds and that our output is more precise in reading decimals rather than rounding whole integers. Thank you for the tips and please let me know if I did anything else incorrectly!
#include <SD.h>
#include <Servo.h>
const int relayPin = 9; // PWM-enabled pin connected to the pump
const int sdChipSelect = 4; // CS pin for the SD card module
Servo pumpControl;
File file;
void setup() {
Serial.begin(9600);
pumpControl.attach(relayPin); // Attach the servo to the specified pin
pinMode(relayPin, OUTPUT);
// Initialize SD card
if (!SD.begin(sdChipSelect)) {
Serial.println("SD card initialization failed!");
while (1);
}
// Open the file
file = SD.open("ttt.txt");
if (file) {
Serial.println("File opened successfully");
} else {
Serial.println("Error opening file");
while (1);
}
}
void loop() {
// Read speed from file
if (file.available()) {
unsigned long speed = file.parseInt(); // Read speed value from the file
setPumpSpeed(speed);
Serial.print("Speed set to: ");
Serial.println(speed);
}
else {
// End of file reached, close and reopen the file
file.close();
file = SD.open("ttt.txt");
}
}
void setPumpSpeed(unsigned long speed) {
// Map the speed value to a PWM range (0-255)
unsigned long pwmValue = map(speed, 0, 1023, 0, 255);
analogWrite(relayPin, pwmValue);
}
Several things. The 9V battery will not cut it and the L298 driver will lose about 3 volts leaving 6 for the motor. Unless you are reversing the pump a simple N-Channel MOSFET would work fine and cut your energy loss to almost zero depending on how you drive it. Get one with a low Vgs (less the 3V) to run the pump.
The link tells you this important facts: Voltage: DC12V/4.8W (AC is forbiden). If the voltage does not match, it will cause the pump to not work or burn out. Red is "+" and black is "-". Please wire it correctly.