Detect Digital Input with Mega 2560

I'm stepping down my 24V signal to a 5V signal using an octocoupler as shown in the attached picture. The 5V signal is powered via a phone charging block (5V, 1A).

The serial monitor on my Arduino sporadically reads a mixture of 0s and 'cycling', no matter whether I am sending the voltage output or not. Any ideas on why I'm not properly getting steady 0s when my voltage output is off or steady 'cycling' when my voltage output is on.

Thanks for any help.

const int PWM = 2;
const int inPin = 52;

void setup() {
  Serial.begin(9600);
  pinMode(PWM, OUTPUT);
  pinMode(inPin, INPUT);
}

void loop() {

Serial.println(digitalRead(inPin));

  while(digitalRead(inPin) == HIGH){
  digitalWrite(PWM, HIGH);
  delay(10);
  digitalWrite(PWM, LOW);
  delay(10);
  Serial.print("Cycling");
  }
  
}

You need a GND to the Arduino from the board.

You can use the charging block to test the 24V side of the isolation module. However, to make use of the opto isolation feature, do not run a ground or 5V connection from the 24V side to the 5V side. Just use the Arduino Mega to power the 5V side ... here ya go ...

That was it. Thank you guys!!!!

You're welcome Girl.

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