Nano with solenoid valve

Hello all. i've been playing around with arduinos for some years now and i regularly use this forum as troubleshhoting all ssrts of problems i've been facinng. But this project revealed way more complicated than i thought and wished.
I built a machine for the rice fields, similar to a sprayer. my aim was to self level the spray bar and later to level the height wth a laser level (not yet installed)

i used an arduino nano to control everything,
A DC-DC Step Down Voltage Regulator as power supply to arduino, mpu and relays
MPU6050 as a gyroscope
4 relay module to control the solenoid valves
An arcade joystick as controller
The solenoid valves coil is 12V30W with math applied gives me 2.5amp consumption.
Knowing that all coils give back loads of voltage i placed correctly one diode on each coil. i dont know the diode reference but they supporrt about 12amp
in a later attempt i even tried to isolate the supply from the arduino, relays and mpu to the other system.
in all the different setups the solenoid valves keep shutting arduino down and after sometimes frying it.
the code is running good.
Any ideas where the damn problem is?

Somewhere in the damn code and damn schematic you didn't damn well post.

3 Likes

Arduino is not a power station, if you connected solenoids directly to pin outputs then it sounds about right

1 Like

I do not bother answering questions like this, I do not like the language. I will give you one hint, your language is a problem. I vote we close this!

  

#include "Wire.h"
#include <MPU6050_light.h>
unsigned long time = 0;

int count=0;

const int RELCIMA = 2;
const int RELBAIXO = 3;
const int RELESQ =  4;
const int RELDIR =  5;
const int JSTKCIMA = 8;
const int JSTKBAIXO = 9;
const int JSTKESQ =  6;
const int JSTKDIR =  7;
const int AUTOBUT =  10;


int AUTOBUTValue = 0;; 
int JSTKCIMAState = 0;
int JSTKBAIXOState = 0;
int JSTKDIRState = 0;
int JSTKESQState = 0;

int atrasoresposta = 350;
const int numReadings = 20;

int readings[numReadings];      // the readings from the analog input
int readIndex = 0;              // the index of the current reading
int total = 0;                  // the running total
int average = 0;                // the average





MPU6050 mpu(Wire);

long timer = 0;

void setup() {

for (int thisReading = 0; thisReading < numReadings; thisReading++) {
    readings[thisReading] = 0;
  }
  

  pinMode(RELCIMA,     OUTPUT);
  pinMode(RELBAIXO,    OUTPUT); 
  pinMode(RELESQ,      OUTPUT);
  pinMode(RELDIR,      OUTPUT); 
  pinMode(JSTKCIMA,    INPUT);
  pinMode(JSTKBAIXO,   INPUT); 
  pinMode(JSTKESQ,     INPUT);
  pinMode(JSTKDIR,     INPUT);
  pinMode(AUTOBUT,     INPUT);
  



  
  Serial.begin(9600);
  Wire.begin();

  byte status = mpu.begin();
  Serial.print(F("MPU6050 status: "));
 Serial.println(status);
  while (status != 0) { 

  } 

  Serial.println(F("Calculating offsets, do not move MPU6050"));
  delay(2000);
  mpu.calcOffsets(true, true); // gyro and accelero
  Serial.println("Done!\n");

}

void loop() {
total = total - readings[readIndex];
  readings[readIndex] = mpu.getAngleX()*1000;
  total = total + readings[readIndex];
  readIndex = readIndex + 1;

  if (readIndex >= numReadings) {
    readIndex = 0;
  }

  average = total / numReadings;
  Serial.println(average);
  delayMicroseconds(500);  



  
  time = millis();



 
  
  

  mpu.update();

  if (millis() - timer > 100) { // print data every second




  Serial.println(mpu.getAngleX());

   //Serial.println(senseAve);
  // Serial.println(AUTOBUTValue);
   //Serial.println(JSTKBAIXOState);
   //Serial.println(JSTKDIRState);
  //Serial.println(JSTKESQState);


    timer = millis();
  }
    JSTKESQState = digitalRead(JSTKESQ);
    JSTKDIRState = digitalRead(JSTKDIR);
    JSTKCIMAState = digitalRead(JSTKCIMA);
    JSTKBAIXOState = digitalRead(JSTKBAIXO);
    AUTOBUTValue = digitalRead(AUTOBUT); 
  

// Serial.println(count);
 
 
//AUTOoo

if (AUTOBUTValue == HIGH){
  
   if (JSTKCIMAState == HIGH){
     digitalWrite(RELCIMA, HIGH); 
     digitalWrite(RELBAIXO, LOW); }

   else {
    digitalWrite(RELCIMA, LOW); }
     

  if (JSTKBAIXOState == HIGH){
     digitalWrite(RELBAIXO, HIGH); 
     digitalWrite(RELCIMA, LOW); }

     else {
    digitalWrite(RELBAIXO, LOW);}
   
   if (JSTKESQState == HIGH){
         digitalWrite(RELESQ, HIGH);
         digitalWrite(RELDIR, LOW);}

else { 
    digitalWrite(RELESQ, LOW); }
    
  if (JSTKDIRState == HIGH){
        digitalWrite(RELDIR, HIGH); 
        digitalWrite(RELESQ, LOW); }
else {
    digitalWrite(RELDIR, LOW); }
                            
  if ((JSTKESQState == LOW) && (JSTKDIRState == LOW)) {
   
   if (average >350 )  {
      while (millis() - timer > atrasoresposta) {}
              digitalWrite(RELESQ, LOW); 
              digitalWrite(RELDIR, HIGH);}
      
       if (average < (-350) )  {
        while (millis() - timer > atrasoresposta) {}
              digitalWrite(RELESQ, HIGH); 
              digitalWrite(RELDIR, LOW);}
               
          
   

 } 
  }

  
  

//NANUAL
    
 if (AUTOBUTValue == LOW){

      if (JSTKESQState == HIGH){
         digitalWrite(RELESQ, HIGH);
         digitalWrite(RELDIR, LOW);}
      else {
        digitalWrite(RELESQ, LOW); }
        
     if (JSTKDIRState == HIGH){
        digitalWrite(RELDIR, HIGH); 
        digitalWrite(RELESQ, LOW); }
else {
        digitalWrite(RELDIR, LOW);
}
     if (JSTKCIMAState == HIGH){
        digitalWrite(RELCIMA, HIGH); 
        digitalWrite(RELBAIXO, LOW); }
else {
        digitalWrite(RELCIMA, LOW); 
   }

    if (JSTKBAIXOState == HIGH){
        digitalWrite(RELBAIXO, HIGH); 
        digitalWrite(RELCIMA, LOW); }
     
     else {
       digitalWrite(RELBAIXO, LOW);}
       }
}

Whats wrong about my language?

probably i didn't explained me that well. the schematics will help you understand the system. please take a look.

you dann right! code and schematics updated. i am not a pro coder and its my first time drawing a scheme in picture so be gentle on my igonorance

I suspect s/he was overreacting to this:

i didn't intend to be rude to anyone except the problems i'm facing. after 2 weeks of trying to sort out the problem working with a passed deadline its just another word for me. but if i can't use minor swearing language i wont use it again...

you are powering nano with 5v to VIN? this is not enough for nano, it needs 7-12 I think

Are you using a pull-up resistor on your buttons? Wiring looks atypical. Normally 1 side connected to arduino with pull-up resistor and other side to ground. Press equals low on arduino pin.

i tried with bigger voltages till 8v, problem still persists

regardless, 5v to VIN is not enough

Look at data sheet to provide regulated power to arduino bypassing onboard regulator. It is hard to make out on the fritzing diagram. A schematic is better. Use online free software like easyEDA or kicad. Alternatively draw it and post a picture

a10k pullup resistor to each button. sorry about the drawing. as i saud before its my first time on fritzing... everything works really fine before i attach the solenoids. i guess the problem is there. after a number of times the whole thing stops, serial monitor stops.... i guess i even fried one of my laptom usb ports while reading serial monitos

Most relay boards allow isolation. I cant make out on the diagram what yours is like but there appears to be a jumper which should be removed. Then you supply logic level from the arduino and relay level from main power supply. Don’t bother getting used to fritzing start learning something better. Fritzing is not useful

So without the solenoids on the switched side of the relays, the relays were working properly, ie the leds on the relay module were behaving and the relays were clicking nicely?

Take a picture of the actual wiring and most importantly the fly back diodes