Isolating WS2812B and DS04-NFC from Arduino using PC817

Hey all,
Please allow a silly question.

I'm trying to make a circuit to run both the motor and the light strip (using fastled) but I need to use an external power source. My question is, do I need to change the code (below) to incorporate an optocoupler? or will it just, kinda work. :astonished:

I'm having trouble visualizing the circuit with my limited knowledge and really don't want to fry anything. Any links to resources or advice would be awesome, thanks.

Thanks in advance.

#include <Servo.h>
#include <FastLED.h>

#define LED_PIN 7
#define NUM_LEDS 60
#define SERVO_PIN 9
#define CW 1900
#define STOP 1450
#define CCW 1000

Servo myServo;
char c;
CRGB leds[NUM_LEDS];

void setup()
{
Serial.begin(57600);
myServo.attach(SERVO_PIN);
myServo.writeMicroseconds(STOP);
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
}

void loop()
{
while (Serial.available() > 0) {
c = Serial.read();

switch (c) {
case 'c':
myServo.writeMicroseconds(CW);
break;
case 'w':
myServo.writeMicroseconds(CCW);
break;
case 's':
myServo.writeMicroseconds(STOP);
break;
}

}
for (int i = 0; i <= 59; i++) {
leds = CRGB ( 0, 0, 255);

  • FastLED.show();*
  • delay(40);*
  • }*
  • for (int i = 19; i >= 0; i--) {*
    _ leds = CRGB ( 255, 0, 0);_
    * FastLED.show();*
    * delay(40);*
    * }*
    }

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html .
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

DSN04-NFC is a continuous rotation servo, not a motor.

Where is the opto-coupler going to be used?
How will you power the Arduino?
What model Arduino are you using?

Thanks.. Tom... :slight_smile:

The only isolation you might need between the Arduino and addressable LEDs is a resistor of about 300-500 ohms. I don't use one in my LED projects.

You could do the same for the servos, but it's not usually needed. The two important things for servos are: first wire the power directly to the servo, and second connect the ground of that power supply to the Arduino ground.

If wired correctly, you don't need to change your code. But the PC817 will not work, it's way too slow. It won't even do 9600 bps Serial, and the signal for your LEDs is at 900 kHz. You'll need something like the 6N137 for such signals. The servo signal should work through a PC817.

But the real question: WHY do you think you need an optocoupler? Normally both applications don't need this, even if you use a separate power supply. It only makes your circuit more complex.

PixelJay:
I'm having trouble visualizing the circuit

Then take out a pencil and paper and start drawing a schematic. That will save you lots of problems down the road.