Speed capture with Photoelectric Sensors, LCD problems

Being a beginner in the Arduino world I've run into problems.

I use four Panasonic Photoelectric Sensor PM-Y45 (NPN type) to measure speed for a free-falling mass. Brown wires connected to +5V, blue wires connected to GND and white wires to D8-D11, with pull-up resistors.

Panasonic Photoelectric Sensor PM-Y45 (NPN type)

I can get reasonable results for all four time intervals, but in some cases I get completely unrealistic results for one or more time for the travel between the sensors, like many thousand milliseconds, (should be around 16-18 milliseconds). I can also get what look like multiples of the actual timestamp, i.e. 2 times or 4 times the real result.

Sometimes (or quite often to be honest) the LCD shows gibberish. Even though I reset the Arduino (by pressing the Reset button) I get gibberish in the LCD.

I didn’t manage to put in the relay in the Fitzing schematic. It is a Velleman WPM406
Velleman WPM406
It’s connected to +18V to middle connection (between NC and NO) and NC to GND (18V) (holding magnet normally activated)
S is connected to pin 13, while + is connected to +5V and is connected to GND (5V).

When the button is pressed the holding magnet is released and the mass is dropped. At the same time, I want a time stamp (start time for time interval between start and first photo sensor).

Following the suggestions to solve LCD-problems I added two capacitators, a 10 µF and a 0,22 µF (.22K, couldn’t find a 0,1 µF), between LCD pin 1 and 2 (VSS and VDO), but the problem still occur just as often.

I use a trim pot for the contrast on the LCD.

I’ve tried both USB and 9V, with the same result.

BN: Arduino Uno
VID: 0x2341
PID: 0x0043
Port: COM3

#include <LiquidCrystal.h>
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

unsigned long timer0;
unsigned long timer1;
unsigned long timer2;
unsigned long timer3;
unsigned long timer4;

float Time01;
float Time12;
float Time23;
float Time34;

int flag1 = 0;
int flag2 = 0;
int flag3 = 0;
int flag4 = 0;

const int BUTTON_PIN = 12;  // Arduino pin connected to button's pin
const int RELAY_PIN = 13;   // Arduino pin connected to relay's pin


void setup() {
  Serial.begin(9600);
  pinMode(8, INPUT);
  pinMode(9, INPUT);
  pinMode(10, INPUT);
  pinMode(11, INPUT);
  pinMode(BUTTON_PIN, INPUT_PULLUP);  // set arduino pin to input pull-up mode
  pinMode(RELAY_PIN, OUTPUT);         // set arduino pin to output mode

  //Inledningsmeddelande i LCD'n
  lcd.begin(16, 2);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Drop Test");
  lcd.setCursor(0, 1);
  delay(1000);
  lcd.clear();
}

void loop() {
  int buttonState = digitalRead(BUTTON_PIN);  // read new state

  if (buttonState == LOW) {
    digitalWrite(RELAY_PIN, HIGH);  // turn on
    timer0 = micros();
  } else if (buttonState == HIGH) {
    digitalWrite(RELAY_PIN, LOW);  // turn off
  }
  if (digitalRead(8) == LOW && flag1 == 0) {
    timer1 = micros();
    flag1 = 1;
  }
  if (digitalRead(9) == LOW && flag2 == 0) {
    timer2 = micros();
    flag2 = 1;
  }

  if (flag1 == 1) { Time01 = timer1 - timer0; }

  if (flag1 == 1 && flag2 == 1) {
    Time12 = timer2 - timer1;
  }
  Time01 = Time01 / 1000;  //convert microsecond to millisecond
  Time12 = Time12 / 1000;  //convert microsecond to millisecond

  if (digitalRead(10) == LOW && flag3 == 0) {
    timer3 = micros();
    flag3 = 1;
  }

  if (flag2 == 1 && flag3 == 1) {
    Time23 = timer3 - timer2;
    Time23 = Time23 / 1000;  //convert microsecond to millisecond
  }

  if (digitalRead(11) == LOW && flag4 == 0) {
    timer4 = micros();
    flag4 = 1;
  }

  if (flag3 == 1 && flag4 == 1) {
    Time34 = timer4 - timer3;
  }
  Time34 = Time34 / 1000;  //convert microsecond to millisecond

  if ((Time01 > 0) && (Time12 > 0) && (Time23 > 0) && (Time34 > 0)) {

    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("t1: ");
    lcd.print(Time01, 2);
    lcd.print(" ms");

    lcd.setCursor(0, 1);
    lcd.print("t2: ");
    lcd.print(Time12, 2);
    lcd.print(" ms");

    delay(7000);

    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("t3: ");
    lcd.print(Time23, 2);
    lcd.print(" ms");

    lcd.setCursor(0, 1);
    lcd.print("t4: ");
    lcd.print(Time34, 2);
    lcd.print(" ms");

    delay(7000);

    lcd.clear();

    flag1 = 0;
    flag2 = 0;
    flag3 = 0;
    flag4 = 0;
  }
}


Most likely directly driving the relay from a Arduino pin is the cause of the issue. The spec does not indicate the current requirements for the relay coil but I suspect they would be more than the Uno can supply.

You might be able to drive the relay with a MOSFET. Look on the internet for circuit diagrams. Use words like "Arduino MOSFET relay"

You'll want additional components for the relay such as a diode to provide inductive kick suppression.

You did try, posting code and "lots of pictures"..... However, Fritzings are toy pictures. A criminal detective might take on the work to find out what the coloured bird nest does. Helpers don't want to see Fritzings. Schematics is the engineering way.

Maybe @Idahowalker hit bulls eye.

Hi, @trainmaniac
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".

This will help with advice on how to present your code and problems.

Can you please post a hand drawn circuit diagram, including power supplies, component names and pin labels?

Thanks.. Tom.. :smiley: :coffee: :australia: :+1:

Confusing connection description. Looking at the image, I would guess you have +18V to COM and NC connected to power the magnet coil, other end of magnet coil connected to GND. This would be considered high side switching.

If so, now when the coil is de-energised (relay energized), there would be a large inductive spike across NO and COM (magnet coils are highly inductive). If you have no kickback diode installed, this would be the main cause of the issues you're experiencing.

Solution: Install a diode connected cathode to NC, anode to COM. The diode's current rating should at minimum match the magnet coil current rating.

Thanks for reply! Working on a schematic, hope to be back soon.

Mats

Confusing description of connections, I agree.
Your guess is right, NC connected to magnetic coil and other end of magnetic coil connected to GND on power supply (Mascot type 8421 12-30 V DC).

There is a diode on the relay but that isn't sufficient then?

The relay was part of an expansion kit to the Arduino, so I assumed that it would work.

The diode on the PCB is for the relay coil. The relay contacts NO/COM/NC are isolated from the control circuit and are unprotected.

Oh, I found a spare diode, so I just went ahead and connected it ...

The diode is not across the relay coil, as a note.

Why?

Just why? :astonished:

To leave room for improvement?

:rofl:

.

Schematic for solution without relay and thereby no timestamp at start:

Schematic for solution with relay aiming for a timestamp at start:
Difficult to connect the relay symbol in the same way as in reality since there is both Signal and +5V on the low voltage side. Relay is powered by 5V.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.