1

I am frustrated with my arduino project can you help me? I am using Arduino Uno, electric lock (high/low), 4 distance sonic modules. Basically I have 4 drawers, I am measuring the distance - how much are they opened and if they are opened in given limits, I set the electric lock open (high).

It is working, but after some while, it stops and it stops running. I have tried to comment all serial communication - didn't help. I have tried to use proper variable data types - same. I have tried to buy much powerful power source - 12V 3,5A and I bought Electrolyte condenzator 4,7mF to balance the power peaks while the lock closes - didn't help.

The lock is turned on using relay, connecting it directly to power source 12V. Relay is powered from 5V arduino supply and controlled by arduino Digital pin. 4 Sonic distance modules are powered from arduino power supply.

Its even more funny that it starts running after ~10 minutes from nothing. Im confused.

Code:

    // defines pins numbers
const int trigPin1 = 2;
const int echoPin1 = 3;
const int trigPin2 = 6;
const int echoPin2 = 5;
const int trigPin3 = 9;
const int echoPin3 = 8;
const int trigPin4 = 12;
const int echoPin4 = 11;
const int relayPin = 7;
//distance limits
const int minSuplik4=27;//suplik4
const int maxSuplik4=29;
const int minSuplik3=22;//suplik3
const int maxSuplik3=24;
const int minSuplik2=32;//suplik2
const int maxSuplik2=34;
const int minSuplik1=9;//suplik1
const int maxSuplik1=11;
/*
double PreDistance1[4]={0,0,0,0};//supliky
double PreDistance2[4]={0,0,0,0};//supliky
double PreDistance3[4]={0,0,0,0};//supliky
*/
long distance[4]={0,0,0,0};//array with distances
int i;
int x;

int trigPinX[4]={trigPin1,trigPin2,trigPin3,trigPin4};//supliky
int echoPinX[4]={echoPin1,echoPin2,echoPin3,echoPin4};//supliky
int maxD[4]={maxSuplik1,maxSuplik2,maxSuplik3,maxSuplik4};//supliky
int minD[4]={minSuplik1,minSuplik2,minSuplik3,minSuplik4};//supliky
void setup() {
pinMode(relayPin, OUTPUT);
pinMode(trigPin1, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin1, INPUT); // Sets the echoPin as an Input
pinMode(trigPin2, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin2, INPUT); // Sets the echoPin as an Input
pinMode(trigPin3, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin3, INPUT); // Sets the echoPin as an Input
pinMode(trigPin4, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin4, INPUT); // Sets the echoPin as an Input
digitalWrite(relayPin, LOW);
Serial.begin(9600); // Starts the serial communication
}
void loop() {

for (int i=0; i<4; i++)
{
  delay(5);
  distance[i]=DistanceFunction(trigPinX[i],echoPinX[i]);
  delay(20);
  /*********** DEBUG ************
  Serial.print("Distance");// Prints the distance on the Serial Monitor
  Serial.print(i+1);
  Serial.print(":");
  Serial.print(distance[i]);
  Serial.print("<");
  Serial.print(minD[i]);
  Serial.print(":");
  Serial.print(maxD[i]);
  Serial.print(">");
  Serial.print(";");
  *********************************/
}

if(distance[0]>=minD[0] && distance[0]<=maxD[0] && distance[1]>=minD[1] && distance[1]<=maxD[1] && distance[2]>=minD[2] && distance[2]<=maxD[2] && distance[3]>=minD[3] && distance[3]<=maxD[3])
      {
        OpenFcn();
      }
else 
      {
        CloseFcn();
      }
delay(100);
}

int DistanceFunction(int trigPin, int echoPin)
{
  digitalWrite(trigPin, LOW);// Clears the trigPin
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);// Sets the trigPin on HIGH state for 10 micro seconds
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  long duration = pulseIn(echoPin, HIGH);// Reads the echoPin, returns the sound wave travel time in microseconds
  long distanceFcn= duration*0.034/2;// Calculating the distance
  return distanceFcn;
}

int OpenFcn()
{
  digitalWrite(relayPin, HIGH);
  //Serial.println("OPENED");
  for (int x=5; x>0; x--)
    {
      //Serial.println("Otevreno:");
      //Serial.println(x);
      delay(1000);
    }
}

int CloseFcn()
{
  digitalWrite(relayPin, LOW);
  //Serial.println("CLOSED");
}

This is the moment when it start working crazy

7
  • 1
    That might be a memory/stack problem or an electrical pulse problem. I can not spot a memory/stack problem, so let's focus on the electric lock and the relay. How much is 4,7mF? I suggest to use 1000µF to the arduino 5v and gnd. If you replace the electric lock or relay with a led plus resistors, does that help? What can you tell about the electric lock and the relay? Can you give a link to them? Please add a message with Serial.println after the Serial.begin in the setup function, so at least you will know if the arduino has reset. – Jot Apr 5 '19 at 23:16
  • Please add all the extra information to your question. A arduino digital pin can give 20mA. The 40mA is the shortcut current and should be avoided. Are you powering a 70mA relay with a digital pin? That is the problem then. Can you give a schematic or drawing or photo of your wiring? – Jot Apr 5 '19 at 23:26
  • please post the serial terminal output as text instead of a picture ...... disconnect the electric lock and see if the program still crashes – jsotola Apr 5 '19 at 23:34
  • I have tried to supply the sonic modules and the relay with external supply, but its the same problem. But I supplied the arduino with different source. Can this be the reason? Relay is this one g7electronica.net/1444-2227-thickbox/rele-srd-05vdc-sl-c.jpg sonic modules are these banggood.com/cs/… – Jasmin Jatić Apr 5 '19 at 23:35
  • Ok so I tried to connect everything on external 5V 3,5A source and only the lock connected to 12V. It still crashes, the same behaviour. – Jasmin Jatić Apr 6 '19 at 9:43
1

It looks like that there was a problem in the relay module or power supply cord. I have changed these two HW elements and looks like it is working. The reason why it did malfuctioning the arduino was, that when i moved the drawer, it slightly moved the power cord which had bad connection, so it made voltage drop on arduino supply and started to do crazy things. Thanks all for your time

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.