Need help synchronizing 2 motors, hall sensor gives false reading

Need help synchronizing 2 motors, hall sensor gives false reading
Basically I am making Sit/Stand table that has 2 motor driven legs (basic bosh DC motors), because load on table can be uneven motors need to be synchronized. It does have magnet ring on geer and I mounted 2 hall sensors (might be slightly off). 34 pin changes per rotation I count them using external interrupts and compare them between motors . It works well when motor is not loaded and NOT switched on/ off frequently . However when code starts to spot one of the motors(using replay to cut power) to compensate for load, the count of rotation gets bad. The number reported by arduino is same on 2 motors but legs are not at the same height (as bad as by 30-40 pulses ). Am I doing something wrong or sensor just gets messed up by motor start/stops.
I was thinking about accelerometer/gyro using filtering to get angle of the table and adjust it to gravity but still don know how it will react to motion of the table.

code sorry its a bit messy i am begginer and just try to make it kind of work and then polish it.

volatile int half_revolutions;
 volatile int lf_revolutions;
volatile int vect;
int rpm;
int rpm2;
 int timeold;
 int timeold2;
 int leveler;
 void setup()
 
 {
   Serial.begin(9600);
   attachInterrupt(0, rpm_fun, FALLING);
   attachInterrupt(1, rpm_2, FALLING);
   //attachInterrupt(0, rpm_fun, CHANGE);
   //attachInterrupt(1, rpm_2, CHANGE);
   half_revolutions = 0;
   lf_revolutions = 0;
  
  
   rpm = 0;
   rpm2 = 0;
   timeold2 = 0;
   timeold = 0;
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
pinMode(8, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);
pinMode(10, INPUT_PULLUP);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);


 }
 void loop()
 {

    while (digitalRead(9)==LOW)
   {
     vect=1;
     
     m1_up();
     m2_up();
     levelup();
     Serial.println(lf_revolutions,DEC);
     Serial.println(half_revolutions,DEC);
     delay(100);
     
   }
   
   m1_stop();
   m2_stop();
   
     while (digitalRead(10)==LOW)
   {
     vect=-1;
     
     m1_dwn();
     m2_dwn();
     leveldwn();
     Serial.println(lf_revolutions,DEC);
     Serial.println(half_revolutions,DEC);
     delay(100);
   }
   
    m1_stop();
   m2_stop();
   
   if (digitalRead(8)==LOW)
   {
     half_revolutions=0;
     lf_revolutions=0;
     Serial.println("reset");
   }
   if (half_revolutions%1==0)
   {
     if (timeold!=half_revolutions)
     {
    Serial.print("M1  Pulses:");   
  Serial.print(half_revolutions,DEC);
  Serial.print("RPM:");
  rpm=half_revolutions/34;
  
     Serial.println(rpm,DEC);
     }
     
   }
   timeold=half_revolutions;
   
      if (lf_revolutions%1==0)
   {
     if (timeold2!=lf_revolutions)
     {
       Serial.print("                M2 Pulses: ");   
  Serial.print(lf_revolutions,DEC);
  
    
  Serial.print("Rotations:");
   rpm2=lf_revolutions/34;
     Serial.println(rpm2,DEC);
     
     
     
   }
   }
   timeold2=lf_revolutions;
   
    
    
   }
   void m1_stop()
   {
     digitalWrite(4, LOW);
     digitalWrite(5, LOW);
     delay(50);
   }
   void m2_stop()
   {
     digitalWrite(6, LOW);
     digitalWrite(7, LOW);
     delay(50);
     
   }
   void m1_up()
   {
     digitalWrite(4, HIGH);
   }
   void m2_up()
   {
     digitalWrite(6, HIGH);
   }
   void m1_dwn()
   {
     digitalWrite(5, HIGH);
   }
   void m2_dwn()
  {
    digitalWrite(7, HIGH);
  }
   void leveldwn()
   {
     leveler=half_revolutions-lf_revolutions;
     if (leveler<-5)
     {
       m2_stop();
     }
     if (leveler>5)
     {
       m1_stop();
     }
     if (digitalRead(9)==LOW)
     {
       leveldwn();
     }
     
     
   }
   void levelup()
   {
     leveler=half_revolutions-lf_revolutions;
     if (leveler<-5)
     {
       m1_stop();
     }
     if (leveler>5)
     {
       m2_stop();
     }
      if (digitalRead(10)==LOW)
     {
            levelup();

     }
     
   }
 void rpm_fun()
 {
   half_revolutions=half_revolutions+vect;
   //Each rotation, this interrupt function is run twice
 }
  void rpm_2()
 {
   lf_revolutions=lf_revolutions+vect;
   //Each rotation, this interrupt function is run twice
 }
//----------------------------------------

Use code tags, the </> icon for posting code. Read the how to use the forum sticky post.

It is likely that you are suffering interference from the motors due to lack of supply decoupling on the motors. See:-
http://www.thebox.myzen.co.uk/Tutorial/De-coupling.html

Ok i fixed code. Regarding the motor decoupling i will defiantly check it out. Just trying to find one which will work with motors that change direction/polarity so i don't explode any capacitors I will try to add 3 capacitors to each motor. add capacitor to hall effect sensor just in case.

I just would like to describe the electrical schematic

I have arduino powered from laptop USB, it has Seed studio relay shield (powered by arduino itself). Hall effect sensors( installed on gearboxes of motors) powered by arduino and pin tiger pin is also pulled up by arduino internally.

The motor power supply is separate 19v 6 amp PSU which connected to the Seed studio relay shield which in turn send power to motors. Other than that there is no any power filtering. The grounds of 2 systems are NOT connected(aduino and 19v PSU.

So you think that motor/relays generate arc/noise on stops which throws off hall effect sensors or arduino chip itself?

Hi, Keep the hall effect wiring away from the motor wiring. You should only need 0.1uF caps, not electrolytic, to do the bypassing.

I was thinking about accelerometer/gyro using filtering to get angle of the table and adjust it to gravity but still don know how it will react to motion of the table.

That would be a good idea, but do not make the leveling automatic, unless you press an up or down button.

rpm, how are you calculating that? I don't see any reference to time keeping?

Tom..... :)

Yes ceramic ones im going to try it next thing tomorrow. RPM was in the code i was basing it on i just did not change it there is no calculations for it, i might need it later to detect stalled motor. I am also trying to get Gyro/ Accelerometer board just to get myself familiar with it. I found article how to get reliable angle reading but it doesn't explain what happens during motion. No no automatic adjustment but maybe i will make a Leveling button :)

I just wondering is it possible that jerking of the motor generates false pules (the same strip of magnet pole read 2 times). I mean mechanically its not very likely as motor connected to warm drive and can't final gear with magnet cant be rotated. Maybe loosing a pulse when motor slows down near it and then speeds up?

UPDATE

Ok guys i cant find 0.1uf capacitors but i found 0.22uf ones so i soldered them between motor terminals, also a added 10uf caps to hall sensor boards.

BUT NOTHING improved, if i place a heavy object on side of the table by 2 up/down cycles (it takes 32 rotations to get from lowest to highest) , table tilts by around 2.5 rotations (34*2=68 pulses is aprox. 2.2 inches) And the motor that stops ends up lower(less rotations) despite reported rotations . Next step i am trying to add some caps to the relay board terminals. Maybe i try to move the relay shield far from arduino (not stacked up).

Guys please advise i am completely lost on this one. :( :( :(

Hi, The magnet ring, can you post a picture of it and how you have mounted the hall effect sensors. Usually these toothed rings are used with an inductive type pickup designed specifically for the job. How far apart are the teeth, how wide is the hall effect pickup. Also put 0.22uF caps across the hall effect with the 10uF, they each have different characteristics.

Tom... :)

Ok I took pictures and noticed that hall effect sensor is a bit off especially if compared to the one that originally was installed. However i tried to move it aground an did not notice any changes in pulses, i will experiment some more.

NOW few things i have noticed leads me to believe that it is something with motors NOISE, i added delay to my interrupts of 5 miliseconds, And right there i got about 3 times improvement (i was able to do about 6 up/down cycles before tilt got out of hand)
So to even it out i decided to manually turn relay of one of the motors and noticed that
every time i turned one motor on OTHER MOTOR’S counter jumped by 2-3 pulses!!! :fearful:
Link to pictures on dropbox

I will capacitor between ground and out on hall sensor (as soon as i find once i can salvage)
I will also try to add some other capacitors maybe a choke coil, or just choke on the cable.
Should i connect grounds of 2 systems together?
Maybe moving relay away from arduino will help too?
Also i will try to change sensor cable from cat 5 to shielded cable that i have from car camera.

I guess that your hall sensors are way too sensitive for their job, and catch fields from the motors or other sources.

UPDATE: Added 8 new 0.1uf capacitors to various places hall effect sensors, arduinos sockets for hall effect sensors, motors terminals on the relay shield. i haven't tried to put ones between motor terminals and its body but i doubt it will change anything. I also tried adding induction coil to motor, did not help at all. However i discovered something new : Inductive Kickback i was holding capacitor terminals with my fingers motor disengaged in i got pretty good electrical shock on my fingers. So now my question is there a way to add Diodes for this application?, i cant just put a diode on motor terminals it will work in only one motor direction. Anyways i am getting another relay shield better remove it from arduino closer to motors. I also thinking adding a high power resistor to create something like 2 speed, so i can slow it down and do soft start/stops. I know motor driver probably better suitable for this but the cheap ones are only rated to 2 amps (1.7 real life application) but maybe bridging 2 together will do the job.

So now my question is there a way to add Diodes for this application?

Yes just look up images for a H-bridge on google and see the four diodes you have to use.

I don't know what circuit you are using so it is time to post a schematic and a photograph of your setup showing all the wiring.

Grumpy_Mike: Yes just look up images for a H-bridge on google and see the four diodes you have to use.

I found one but they use transistors not relays. where when power not applied all gates are open i use relays |500x260 so at all times 2 diodes will be shorted by relay. rendering them useless. I may be wrong on this one i am not an expert on circuits. I will try to draw a full one.

You cannot simply switch off a coil (relay, motor...), like a lamp or some other ohmic resistor. You can try to do that, but then you'll notice weird impulses in your circuit, sparks on the switching relay contacts, or simply a burned switching transistor.

When a motor (or coil) is suddenly powered off, by a relay or transistor, it acts as a generator that sources the same current as it was sinking before. That current causes a high and reversed voltage on the now open contacts, which must be eliminated somehow. That's what the diodes over the switches do: they feed the reverse current back into the power source.

DrDiettrich: That's what the diodes over the switches do: they feed the reverse current back into the power source.

Can it destroy Switching power supply? Right now i have something like this linkbut in my understanding it only blocks half of the surge (half of ac wave). i found something mentioning Varistors can it be used? i saw it on one of the schematics but nowhere else.

Just use four diodes like the transistor h-bridge. That will protect against back emf. Each transistor is the equivalent of a relay contact.

so at all times 2 diodes will be shorted by relay. rendering them useless. I may be wrong on this one i am not an expert on circuits.

Yes at any one time two diodes are shorted out and they are, at that time useless and the other two are functioning. When the contacts change over they are not useless it is the other two. So the diodes function in pairs at a time.

UPDATE:
Ok i did use the schematics as for transistors and everything works fine now no phantom pluses. :slight_smile:
Im just unsure if power supply is design to handle such reverse loads. Its 19v DC 6.3 amp brick used for thermal label printer printer.
Anyways thanks for all your help guys.

Hi, dimammx, do not use the circuit of MOSFET H-Bridge in post #11. If the input signal is only 5V, the top P-CH Mosfets will not turn off with the 9V supply.

Tom... :)

TomGeorge
Yea i am still using relays i meant i put diodes same way as in transistors schematic.
Actually i think because i use relays and one of the contacts is always connected i can just use 2 diodes instead of 4 (after all 1 of them is shorted by the relay at any given time).

after all 1 of them is shorted by the relay at any given time).

The same one?

Now that i think about that's probably wrong. but thought something like this might work https://www.dropbox.com/s/7fbzcqsfka8jl3d/2015-08-10%2003.30.52.jpg?dl=0 only 1 relay is switched at a time so one of the motor terminals is always connected to +19