Skip to content

Instantly share code, notes, and snippets.

@sankarcheppali
Created January 22, 2024 03:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sankarcheppali/c327c786380f304eda039697aa1d4845 to your computer and use it in GitHub Desktop.
Save sankarcheppali/c327c786380f304eda039697aa1d4845 to your computer and use it in GitHub Desktop.
dc water pump speed control using arduino
/**
* receive command from serial and contorl motor speed by setting pwm duty
*/
// motor A Control pins (output 1,ouptut 2)
#define IN1 5
#define IN2 6
#define ENA 3
void setup() {
pinMode(IN1,OUTPUT);
pinMode(IN2,OUTPUT);
pinMode(ENA,OUTPUT);
// put the motor in forward direction
digitalWrite(IN1,HIGH);
digitalWrite(IN2,LOW);
// set default speed
analogWrite(ENA,100);
Serial.begin(115200);
}
void loop() {
if (Serial.available()) {
String inputString = Serial.readStringUntil('\n');
if(inputString.startsWith(":")){
String sliderValueStr = inputString.substring(1); // extract the angle
int sliderValue = strtol(sliderValueStr.c_str(), NULL, 10); // convert str to int
Serial.print("Received angle set cmd ");
Serial.println(sliderValue);
analogWrite(ENA,map(sliderValue, 0, 180, 0, 255));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment