Go Down

Topic: analogWrite(3, 255) == digitalWrite(3, HIGH)?? (Read 1 time) previous topic - next topic


Hello, I am trying to drive a opto-isolater from my arduino digital port and it is not able to drive the LED (the one within the opto-isolator ofcourse) when I use
Code: [Select]
digitalWrite(3, HIGH), but it does work when I use
Code: [Select]
analogWrite(3, 255).

What is the big "hardware" difference here? To my opinion it should have been the same or is:  
Code: [Select]
digitalWrite(3, HIGH) == analogWrite(3, 127) ,true?

But it I should have worked using the digital way, setting the pinMode to OUTPUT and throwing it HIGH, right?

For the record:
Using this opto-isolator: MOC3041
connecting PIN1 to arduino digital port 3
port 3 is set to OUTPUT
connecting PIN2 via 1kOhm resistor to arduino ground

Already did some testing with a multimeter and different setups using different resistors but, I am not really a professional when it comes to debugging hardware.

If I forgot to mention some important detail, please feel free to inquire me.


Jun 16, 2010, 01:57 am Last Edit: Jun 16, 2010, 01:59 am by retrolefty Reason: 1
Not sure it's the source of your problem or not (no complete code or wiring drawing posted), but the data sheet ( http://www.datasheetcatalog.org/datasheet/fairchild/MOC3041-M.pdf ) for the 3041 shows a typical operating condition for the input led as forward voltage drop = 1.25 and forward current of 30ma. So (5vdc-1.25) / .030 = 125 ohms for the current limiting resistor. A 1k ohm resistor would result in only 3ma of current.

This is stated in the data sheet "Therefore, recommended operating IF lies between
max IFT (15 mA for MOC3031M & MOC3041M, 10 mA for MOC3032M & MOC3042M, 5 mA for MOC3033M & MOC3043M) and
absolute max IF (60 mA)."

So maybe try lowering the resistor value such that the led forward current in is the 20-30ma range.



Thanks I am going to try this right away. The basic of the schematic is this

the "triac" side looks a little bit different in my schematic but, that part works.

My code (last simplified version used)
Code: [Select]
//pin numbers
#define EL_WIRE_1 3
#define EL_WIRE_2 5

#define DELAY_SHORT 500

int relay_state, delaytime;
unsigned long int prev_mill, curr_mill;

void setup(){
 pinMode(EL_WIRE_1, OUTPUT);
 pinMode(EL_WIRE_2, OUTPUT);

 prev_mill = 0;
 relay_state = LOW;

void loop(){
 curr_mill = millis();

 if(curr_mill - prev_mill < DELAY_SHORT) {
   if(relay_state == LOW) relay_state = HIGH;
 else {
   relay_state = LOW;
 digitalWrite(EL_WIRE_1, relay_state);

Could you either way elaborate on that calculation you're making, I did wonder how one could calculate the resistor needed in such applications. Like what data should you look for in general?


Could you either way elaborate on that calculation you're making

If the LED in the opto drops 1.25V then there must be 5 - 1.2 = 3.8V dropped across the resistor.
In order to have 30mA flowing through a resistor with 3.8V across it the resistor value must be 3.8 / 0.03 = 126.666666 ohms
Which is a value you can't buy so the closest would be 125R.

As to the original problem using analogue write automatically sets the pin to be an output where as the digital write requires it to be set separately. I know the code you posted does this but is there any chance this got missed originally?


cool thanks for that explanation, saved the data for later use ;)

Well, on topic. Eventually found out that I had an erroneous arduino when I used the same setup and code with my own arduino (which I didn't had lying around when I was debugging). [crap! spent 2 full days on trying to solve this problem]

The erroneous one also heats up the ATmel chip when I connect it to 12V (no problem connecting to 9V by the way). It's definitely a goner, probably blown-up by it's owner.

Could I either way use your knowledge for one more question? It is one out of pure curiosity, I wondered what this thing was: IRF840 they call it a mosfet/hexfet. I am also curious in what a mosfet is.

If you're to busy, just ignore my topic and I want to thank you guys for helping me out and sharing some of your knowledge with me.


I am also curious in what a mosfet is.

Basically it is a switch. When it is on it has a very low resistance. It uses voltages to switch as opposed to transistors that use current to switch.


Not the most useful MOSFET for typical Arduino applications. It's a high voltage device, 500v / 8 amps max and also needs standard mosfet gate voltage, +10vdc to fully saturate on. A better power mosfet device would be a LOGIC level mosfet rated at say 100 volts / 25-30 amps.

Logic level mosfets are my favorite devices to use when output switching requirements exceed +5vdc @ 30 ma.


Go Up