0
\$\begingroup\$

I am porting an Arduino project to Raspberry Pi.

On the Arduino I switched two 12V solenoid Valves with the help of a L293D IC. It worked perfectly.

Now I am porting this system to a Raspberry Pi.

The switching does not work properly. Sometimes it switches, sometimes not. Since I am a beginner I first ask if there is some condition I don't see. I know that the GPIO pins work with 3.3V, but that should be enough according to the L293D data sheet.

Unfortunately I can't find a data sheet for the valves. I was also thinking if there is some issue with the PWM for the Enable pin. I have not configured anything regarding that in the code. I just connected a PWM pin from the RasPi to the L293D thinking that it will work just like on the Arduino. (I don't really need PWM since I use Enable pin also for switch on and off)

I wrote the program in C with the Wiring Pi library. Relevant code:

#include <wiringPi.h>

#define BUTTON_1_PIN 4
#define BUTTON_2_PIN 25
#define DRIVER_ENABLE_0_PIN 18 // air pump
#define DRIVER_ENABLE_1_PIN 12  // valve_1
#define DRIVER_ENABLE_2_PIN 13 // valve_2
#define VALVE_1_VCC_PIN 6
#define VALVE_1_GND_PIN 5
#define VALVE_2_VCC_PIN 23
#define VALVE_2_GND_PIN 24
#define AIRPUMP_VCC_PIN 22
#define AIRPUMP_GND_PIN 27

int button_1_state = HIGH;
int button_2_state = HIGH;

void setup() {
  wiringPiSetupGpio();

  pinMode(BUTTON_1_PIN, INPUT);
  pinMode(BUTTON_2_PIN, INPUT);

  pullUpDnControl(BUTTON_1_PIN, PUD_UP);
  pullUpDnControl(BUTTON_2_PIN, PUD_UP);

  pinMode(DRIVER_ENABLE_0_PIN, OUTPUT);
  pinMode(AIRPUMP_VCC_PIN, OUTPUT);
  pinMode(AIRPUMP_GND_PIN, OUTPUT);
  pinMode(DRIVER_ENABLE_1_PIN, OUTPUT);
  pinMode(VALVE_1_VCC_PIN, OUTPUT);
  pinMode(VALVE_1_GND_PIN, OUTPUT);
  pinMode(DRIVER_ENABLE_2_PIN, OUTPUT);
  pinMode(VALVE_2_VCC_PIN, OUTPUT);
  pinMode(VALVE_2_GND_PIN, OUTPUT);

  digitalWrite(DRIVER_ENABLE_0_PIN, LOW);
  digitalWrite(AIRPUMP_VCC_PIN, HIGH);
  digitalWrite(AIRPUMP_GND_PIN, LOW);
  digitalWrite(DRIVER_ENABLE_1_PIN, LOW);
  digitalWrite(VALVE_1_VCC_PIN, HIGH);
  digitalWrite(VALVE_1_GND_PIN, LOW);
  digitalWrite(DRIVER_ENABLE_2_PIN, LOW);
  digitalWrite(VALVE_2_VCC_PIN, HIGH);
  digitalWrite(VALVE_2_GND_PIN, LOW);
}

void loop() {
  button_1_state = digitalRead(BUTTON_1_PIN);
  button_2_state = digitalRead(BUTTON_2_PIN);

  if (button_1_state == LOW) {
    digitalWrite(DRIVER_ENABLE_0_PIN, HIGH); // turn on air pump
    digitalWrite(DRIVER_ENABLE_1_PIN, HIGH); // switch valve_1 open
    digitalWrite(DRIVER_ENABLE_2_PIN, LOW);  // switch valve_2 close
  } else {
    if (button_2_state == LOW) {
      digitalWrite(DRIVER_ENABLE_0_PIN, HIGH); // turn on air pump
      digitalWrite(DRIVER_ENABLE_1_PIN, LOW);  // switch valve_1 close
      digitalWrite(DRIVER_ENABLE_2_PIN, HIGH); // switch valve_2 open
    } else {
      digitalWrite(DRIVER_ENABLE_1_PIN, LOW); // switch valve_1 close
      digitalWrite(DRIVER_ENABLE_2_PIN, LOW); // switch valve_2 close
      digitalWrite(DRIVER_ENABLE_0_PIN, LOW); // turn off air pump
    }
  }
}

int main(void) {
  setup();
  while (1) {
    loop();
  }
  return 0;
}
\$\endgroup\$

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.