0

enter image description hereThank you for the add, total newbie here (electronics and arduino) on his first project.

Trying to make a counter for a hay baler with two HAL effect sensors, (pin 5 & 6) and outputting to an I2C display. One counts the amount of flakes per bale and the other the amount of bales (resetting the flakes on each bale and displaying an average)

I works perfectly whilst powered through USB, plug the 12v in and the counter just goes nuts and keeps counting without the magnet passing by the sensor.

  • I have checked if there is 5v at the 5v pin.
  • As soon as you take the trigger out of pin 5 or 6 the corresponding counter stops, put anything in the pin (even a bit of wire or a diode) and the counter starts taking off again.
//Bale and flake Counter
//Lourens Houttuin    
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
//i2c pins
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // 
// set up a constant for the tilt switchPin
const int flakePin = 6;
const int balePin = 7;

int bales = 0;
int flakes = 0;
int avflakes = 0;

// variable to hold the value of the flakePin & bale pin
int switchState = 0;
int baleswitchState = 0;

// variable to hold previous value of the flakepin
int prevSwitchState = 0;
int prevBaleswitchstate = 0;

void setup() {  
  // set up the number of columns and rows on the LCD 
  lcd.begin(16, 2);

  // set up the switch pin as an input
  pinMode(flakePin,INPUT);
  pinMode(balePin,INPUT);

 lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Flakes: 0");
}

void loop() {  
  // check the status of the switch
  switchState = digitalRead(flakePin);
  baleswitchState = digitalRead(balePin);

  // compare the switchState to its previous state
  if (baleswitchState != prevBaleswitchstate){
    if (baleswitchState == LOW) {
      bales = bales + 1;
      flakes = 0;
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Flakes:");
      lcd.setCursor(8, 0);
      lcd.print(flakes);
      lcd.print("/");
      lcd.print(avflakes/bales);
      lcd.setCursor (0,1);
      lcd.print("Bales:");
      lcd.setCursor (8,1);
      lcd.print(bales);
    }
  }
  if (switchState != prevSwitchState) {
    if (flakes>13){
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Flakes:");
      lcd.setCursor(8, 0);
      lcd.print("Speed UP");
    }
    if (switchState == LOW) {
      flakes = flakes + 1;
      avflakes=avflakes+1;
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Flakes:");
      lcd.setCursor(8, 0);
      lcd.print(flakes);
      lcd.print("/");
      lcd.print(avflakes/bales);
      lcd.setCursor (0,1);
      lcd.print("Bales:");
      lcd.setCursor (8,1);
      lcd.print(bales);
    }
  }
  // save the current switch state as the last state 
  prevSwitchState = switchState;
  prevBaleswitchstate = baleswitchState;
}
  • Edit your code to post properly - indent the lines with 4 spaces I think will do it. Is it 12V DC, not AC? The regulator and caps aren't really set up for heavy duty AC/DC conversion. – CrossRoads Dec 18 '18 at 19:07
  • 2
    Can you show your wiring? What sort of 12V supply is it? Perhaps the supply is outputting noise which is triggering the Hall effect sensors. – Nick Gammon Dec 18 '18 at 19:37
  • 1
    Are your Hall Effect sensors logic output or analog?I have seen both types -- logic output will have HIGH or LOW only, but analog will vary the output voltage in accordance with the strength and polarity of the magnetic field. If a logic output, then is it open-collector and thus needs a pull-down or pull-up? – jose can u c Dec 18 '18 at 19:46
  • I'll try fix the code and upload schematic when I get to my computer. I have 12v dc 1.25A I believe The HAL Sensors are analog ( correct me if iam wrong) because I was told I had to put a 10 k resistor in to stop the fluctuations in voltage as the magnet approaches. But it doesn't matter if it's the sensor plugged in or a bit of wire as soon as either pin has anything in it the sketch starts running and continues to count until that wire is pulled out of the pin – Lumpy Dec 18 '18 at 21:31
  • Hope the schematic is right, I used fritzing as I don't know yet how to do them any other way. I have put a 9V battery instead of the 12v power supply, it seems to be working ok, so next try it out on vehicle 12v. – Lumpy Dec 19 '18 at 8:11
0

Is the 12v supply from a running vehicle? And the USB is from a computer? Is the Arduino in the same location for both tests or is it on your desk with the USB and on the vehicle for the 12v test?

Vehicle power is notoriously noisy - varying voltages, voltage spikes. I have run an Arduino successfully in my car supplied from a 5v USB charger in the cigarette lighter socket. Even with a thermo-sensor on a piece of Cat-5 cable coming out of the hood and through the driver's window (don't ask! :) I had quite steady readings and my display (I2C OLED) was solid. But do expect most external wiring to act like an antenna and pick up some of that noise. You might have to do some data smoothing or averaging to clean it up.

Update:

The eventual finished unit will be mounted in a tractor with the sensors about 4 meters back

See if a car-USB charger fixes the problem on the bench. Does the tractor have a "cigarette lighter socket" power-point as a car does? If not, they are available as a standalone part.

Is it ignition-suppressed to not interfere with a radio, the way a car is? Or does the baler have its own engine (which is most probably not suppressed)? If no suppression, you may need to put the Arduino in a metal box, grounded to the tractor frame, and use a shielded cable between the box and the sensors. And if it's still noisy, possibly even locate the box closer to the sensors - that cable will be like an antenna for noise; you'll need to minimize it.

  • At the moment a fixed power supply in same location. – Lumpy Dec 19 '18 at 0:13
  • The eventual finished unit will be mounted in a tractor with the sensors about 4 meters back on a baler to count the plunger strokes and bale numbers, so as long as the power is filtered iam hoping it all be good (dont know about the cheapy light bar but) – Lumpy Dec 19 '18 at 7:35

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.