Having trouble in GPIO output, 220 V to 5 V signal

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite












I'm not an electric guy, I'm just trying to build a small circuit to sense 220 V with my Microcontroller (RPi). But I'm getting either 5 V or 1.3 V on a GPIO wire.



I can't understand the problem. I want 0 V or 5 V on GPIO.



enter image description here










share|improve this question









New contributor




Ghazanfar Javed is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • The leakage current out of your GPIO pin in input mode has to be conducted to 0 V by the pull-down resistor. It's often something like 3..10 uA, so the voltage drop it causes across the 370K may well cause your 1.3 V. Change it to 10K and try again.
    – TonyM
    2 hours ago














up vote
2
down vote

favorite












I'm not an electric guy, I'm just trying to build a small circuit to sense 220 V with my Microcontroller (RPi). But I'm getting either 5 V or 1.3 V on a GPIO wire.



I can't understand the problem. I want 0 V or 5 V on GPIO.



enter image description here










share|improve this question









New contributor




Ghazanfar Javed is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • The leakage current out of your GPIO pin in input mode has to be conducted to 0 V by the pull-down resistor. It's often something like 3..10 uA, so the voltage drop it causes across the 370K may well cause your 1.3 V. Change it to 10K and try again.
    – TonyM
    2 hours ago












up vote
2
down vote

favorite









up vote
2
down vote

favorite











I'm not an electric guy, I'm just trying to build a small circuit to sense 220 V with my Microcontroller (RPi). But I'm getting either 5 V or 1.3 V on a GPIO wire.



I can't understand the problem. I want 0 V or 5 V on GPIO.



enter image description here










share|improve this question









New contributor




Ghazanfar Javed is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I'm not an electric guy, I'm just trying to build a small circuit to sense 220 V with my Microcontroller (RPi). But I'm getting either 5 V or 1.3 V on a GPIO wire.



I can't understand the problem. I want 0 V or 5 V on GPIO.



enter image description here







sensor ac gpio 5v






share|improve this question









New contributor




Ghazanfar Javed is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Ghazanfar Javed is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 1 hour ago









Michel Keijzers

4,75062149




4,75062149






New contributor




Ghazanfar Javed is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 3 hours ago









Ghazanfar Javed

111




111




New contributor




Ghazanfar Javed is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Ghazanfar Javed is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Ghazanfar Javed is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











  • The leakage current out of your GPIO pin in input mode has to be conducted to 0 V by the pull-down resistor. It's often something like 3..10 uA, so the voltage drop it causes across the 370K may well cause your 1.3 V. Change it to 10K and try again.
    – TonyM
    2 hours ago
















  • The leakage current out of your GPIO pin in input mode has to be conducted to 0 V by the pull-down resistor. It's often something like 3..10 uA, so the voltage drop it causes across the 370K may well cause your 1.3 V. Change it to 10K and try again.
    – TonyM
    2 hours ago















The leakage current out of your GPIO pin in input mode has to be conducted to 0 V by the pull-down resistor. It's often something like 3..10 uA, so the voltage drop it causes across the 370K may well cause your 1.3 V. Change it to 10K and try again.
– TonyM
2 hours ago




The leakage current out of your GPIO pin in input mode has to be conducted to 0 V by the pull-down resistor. It's often something like 3..10 uA, so the voltage drop it causes across the 370K may well cause your 1.3 V. Change it to 10K and try again.
– TonyM
2 hours ago










3 Answers
3






active

oldest

votes

















up vote
5
down vote













Here is using your basic idea, but a circuit that will actually work:





Note the addition of D2. That's important to keep the LED from frying. Yes, D1 does block current in the reverse direction, but it will also have some inevitable leakage. The leakage current should be small (see the datasheet), but that could be enough to build up enough voltage across the LED, also in reverse then, to exceed the LED's reverse voltage spec. D2 provides a safe shunt path for whatever leakage current there may be thru D1 during the negative half-cycle of V1.



Unlike another answer, I recommend keeping D1. With D2 across the LED, D1 can actually leak up to being a short without harm to the LED or D2. You could therefore leave it off. However, it serves a useful purpose in reducing the power dissipation. The diode drops are small compared to 220 V, so you basically have 220 V across a 200 kΩ resistor. That comes out to ¼ W. With D1 in there, that is reduced by half, since current is only flowing during half the V1 voltage cycle. Now you can safely use a ¼ W resistor and still allow for some insulation, packaging, limited air flow, etc, around it. Or you could lower R1 to get more LED current, and thereby require less current transfer ratio of the opto.



On the digital side, all you need is a pullup resistor. But, you need to check the current capability all along the way. The peak voltage of a 220 V sine is 311 V. Let's say 309 V across R1 due to the voltage drop of the LED. That results in 1.5 mA. Of course you don't just want a short blip at the peak of the positive power cycles, so let's say anything over 1.0 mA into the LED should assert the output.



The R2 pullup resistor requires 500 µA for the GPIO line to be held low. That means this circuit would work with a opto-coupler with a CTR (current transfer ratio) of at least ½. Optos with a minimum CTR of 1 are easy to find, especially when fast response is not required, like in this case. You therefore have a fairly comfortable margin of 2x with a 1:1 opto-coupler. That all sounds good.






share|improve this answer



























    up vote
    2
    down vote













    Try it like this:



    enter image description here



    There are two main differences:



    1. There's a diode in antiparallel to the optoisolator's LED. This is better reverse polarity protection than the series diode you used.


    2. The output of the optoisolator pulls the GPIO to ground. This gets you a clear high/low. It will provide a low to the GPIO when AC is present. Because of R2 and C3, it will immediately go low when AC is applied, but will take a bit of time to go high once the AC is gone.






    share|improve this answer





























      up vote
      0
      down vote













      Remove what you have on the output side of U3 at the moment. Connect pin 3 of U3 to ground and pull up pin 4 to VCC with something like a 100k resistor. Add a capacitor between pin 4 and VCC if the output needs smoothing.



      You are trying to use npn transistors as high side switches. This is not something you should try to do as a novice.






      share|improve this answer




















        Your Answer




        StackExchange.ifUsing("editor", function ()
        return StackExchange.using("mathjaxEditing", function ()
        StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
        StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
        );
        );
        , "mathjax-editing");

        StackExchange.ifUsing("editor", function ()
        return StackExchange.using("schematics", function ()
        StackExchange.schematics.init();
        );
        , "cicuitlab");

        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "135"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        convertImagesToLinks: false,
        noModals: false,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        bindNavPrevention: true,
        postfix: "",
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );






        Ghazanfar Javed is a new contributor. Be nice, and check out our Code of Conduct.









         

        draft saved


        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2felectronics.stackexchange.com%2fquestions%2f396825%2fhaving-trouble-in-gpio-output-220-v-to-5-v-signal%23new-answer', 'question_page');

        );

        Post as a guest






























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        5
        down vote













        Here is using your basic idea, but a circuit that will actually work:





        Note the addition of D2. That's important to keep the LED from frying. Yes, D1 does block current in the reverse direction, but it will also have some inevitable leakage. The leakage current should be small (see the datasheet), but that could be enough to build up enough voltage across the LED, also in reverse then, to exceed the LED's reverse voltage spec. D2 provides a safe shunt path for whatever leakage current there may be thru D1 during the negative half-cycle of V1.



        Unlike another answer, I recommend keeping D1. With D2 across the LED, D1 can actually leak up to being a short without harm to the LED or D2. You could therefore leave it off. However, it serves a useful purpose in reducing the power dissipation. The diode drops are small compared to 220 V, so you basically have 220 V across a 200 kΩ resistor. That comes out to ¼ W. With D1 in there, that is reduced by half, since current is only flowing during half the V1 voltage cycle. Now you can safely use a ¼ W resistor and still allow for some insulation, packaging, limited air flow, etc, around it. Or you could lower R1 to get more LED current, and thereby require less current transfer ratio of the opto.



        On the digital side, all you need is a pullup resistor. But, you need to check the current capability all along the way. The peak voltage of a 220 V sine is 311 V. Let's say 309 V across R1 due to the voltage drop of the LED. That results in 1.5 mA. Of course you don't just want a short blip at the peak of the positive power cycles, so let's say anything over 1.0 mA into the LED should assert the output.



        The R2 pullup resistor requires 500 µA for the GPIO line to be held low. That means this circuit would work with a opto-coupler with a CTR (current transfer ratio) of at least ½. Optos with a minimum CTR of 1 are easy to find, especially when fast response is not required, like in this case. You therefore have a fairly comfortable margin of 2x with a 1:1 opto-coupler. That all sounds good.






        share|improve this answer
























          up vote
          5
          down vote













          Here is using your basic idea, but a circuit that will actually work:





          Note the addition of D2. That's important to keep the LED from frying. Yes, D1 does block current in the reverse direction, but it will also have some inevitable leakage. The leakage current should be small (see the datasheet), but that could be enough to build up enough voltage across the LED, also in reverse then, to exceed the LED's reverse voltage spec. D2 provides a safe shunt path for whatever leakage current there may be thru D1 during the negative half-cycle of V1.



          Unlike another answer, I recommend keeping D1. With D2 across the LED, D1 can actually leak up to being a short without harm to the LED or D2. You could therefore leave it off. However, it serves a useful purpose in reducing the power dissipation. The diode drops are small compared to 220 V, so you basically have 220 V across a 200 kΩ resistor. That comes out to ¼ W. With D1 in there, that is reduced by half, since current is only flowing during half the V1 voltage cycle. Now you can safely use a ¼ W resistor and still allow for some insulation, packaging, limited air flow, etc, around it. Or you could lower R1 to get more LED current, and thereby require less current transfer ratio of the opto.



          On the digital side, all you need is a pullup resistor. But, you need to check the current capability all along the way. The peak voltage of a 220 V sine is 311 V. Let's say 309 V across R1 due to the voltage drop of the LED. That results in 1.5 mA. Of course you don't just want a short blip at the peak of the positive power cycles, so let's say anything over 1.0 mA into the LED should assert the output.



          The R2 pullup resistor requires 500 µA for the GPIO line to be held low. That means this circuit would work with a opto-coupler with a CTR (current transfer ratio) of at least ½. Optos with a minimum CTR of 1 are easy to find, especially when fast response is not required, like in this case. You therefore have a fairly comfortable margin of 2x with a 1:1 opto-coupler. That all sounds good.






          share|improve this answer






















            up vote
            5
            down vote










            up vote
            5
            down vote









            Here is using your basic idea, but a circuit that will actually work:





            Note the addition of D2. That's important to keep the LED from frying. Yes, D1 does block current in the reverse direction, but it will also have some inevitable leakage. The leakage current should be small (see the datasheet), but that could be enough to build up enough voltage across the LED, also in reverse then, to exceed the LED's reverse voltage spec. D2 provides a safe shunt path for whatever leakage current there may be thru D1 during the negative half-cycle of V1.



            Unlike another answer, I recommend keeping D1. With D2 across the LED, D1 can actually leak up to being a short without harm to the LED or D2. You could therefore leave it off. However, it serves a useful purpose in reducing the power dissipation. The diode drops are small compared to 220 V, so you basically have 220 V across a 200 kΩ resistor. That comes out to ¼ W. With D1 in there, that is reduced by half, since current is only flowing during half the V1 voltage cycle. Now you can safely use a ¼ W resistor and still allow for some insulation, packaging, limited air flow, etc, around it. Or you could lower R1 to get more LED current, and thereby require less current transfer ratio of the opto.



            On the digital side, all you need is a pullup resistor. But, you need to check the current capability all along the way. The peak voltage of a 220 V sine is 311 V. Let's say 309 V across R1 due to the voltage drop of the LED. That results in 1.5 mA. Of course you don't just want a short blip at the peak of the positive power cycles, so let's say anything over 1.0 mA into the LED should assert the output.



            The R2 pullup resistor requires 500 µA for the GPIO line to be held low. That means this circuit would work with a opto-coupler with a CTR (current transfer ratio) of at least ½. Optos with a minimum CTR of 1 are easy to find, especially when fast response is not required, like in this case. You therefore have a fairly comfortable margin of 2x with a 1:1 opto-coupler. That all sounds good.






            share|improve this answer












            Here is using your basic idea, but a circuit that will actually work:





            Note the addition of D2. That's important to keep the LED from frying. Yes, D1 does block current in the reverse direction, but it will also have some inevitable leakage. The leakage current should be small (see the datasheet), but that could be enough to build up enough voltage across the LED, also in reverse then, to exceed the LED's reverse voltage spec. D2 provides a safe shunt path for whatever leakage current there may be thru D1 during the negative half-cycle of V1.



            Unlike another answer, I recommend keeping D1. With D2 across the LED, D1 can actually leak up to being a short without harm to the LED or D2. You could therefore leave it off. However, it serves a useful purpose in reducing the power dissipation. The diode drops are small compared to 220 V, so you basically have 220 V across a 200 kΩ resistor. That comes out to ¼ W. With D1 in there, that is reduced by half, since current is only flowing during half the V1 voltage cycle. Now you can safely use a ¼ W resistor and still allow for some insulation, packaging, limited air flow, etc, around it. Or you could lower R1 to get more LED current, and thereby require less current transfer ratio of the opto.



            On the digital side, all you need is a pullup resistor. But, you need to check the current capability all along the way. The peak voltage of a 220 V sine is 311 V. Let's say 309 V across R1 due to the voltage drop of the LED. That results in 1.5 mA. Of course you don't just want a short blip at the peak of the positive power cycles, so let's say anything over 1.0 mA into the LED should assert the output.



            The R2 pullup resistor requires 500 µA for the GPIO line to be held low. That means this circuit would work with a opto-coupler with a CTR (current transfer ratio) of at least ½. Optos with a minimum CTR of 1 are easy to find, especially when fast response is not required, like in this case. You therefore have a fairly comfortable margin of 2x with a 1:1 opto-coupler. That all sounds good.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 1 hour ago









            Olin Lathrop

            276k28329775




            276k28329775






















                up vote
                2
                down vote













                Try it like this:



                enter image description here



                There are two main differences:



                1. There's a diode in antiparallel to the optoisolator's LED. This is better reverse polarity protection than the series diode you used.


                2. The output of the optoisolator pulls the GPIO to ground. This gets you a clear high/low. It will provide a low to the GPIO when AC is present. Because of R2 and C3, it will immediately go low when AC is applied, but will take a bit of time to go high once the AC is gone.






                share|improve this answer


























                  up vote
                  2
                  down vote













                  Try it like this:



                  enter image description here



                  There are two main differences:



                  1. There's a diode in antiparallel to the optoisolator's LED. This is better reverse polarity protection than the series diode you used.


                  2. The output of the optoisolator pulls the GPIO to ground. This gets you a clear high/low. It will provide a low to the GPIO when AC is present. Because of R2 and C3, it will immediately go low when AC is applied, but will take a bit of time to go high once the AC is gone.






                  share|improve this answer
























                    up vote
                    2
                    down vote










                    up vote
                    2
                    down vote









                    Try it like this:



                    enter image description here



                    There are two main differences:



                    1. There's a diode in antiparallel to the optoisolator's LED. This is better reverse polarity protection than the series diode you used.


                    2. The output of the optoisolator pulls the GPIO to ground. This gets you a clear high/low. It will provide a low to the GPIO when AC is present. Because of R2 and C3, it will immediately go low when AC is applied, but will take a bit of time to go high once the AC is gone.






                    share|improve this answer














                    Try it like this:



                    enter image description here



                    There are two main differences:



                    1. There's a diode in antiparallel to the optoisolator's LED. This is better reverse polarity protection than the series diode you used.


                    2. The output of the optoisolator pulls the GPIO to ground. This gets you a clear high/low. It will provide a low to the GPIO when AC is present. Because of R2 and C3, it will immediately go low when AC is applied, but will take a bit of time to go high once the AC is gone.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited 1 hour ago

























                    answered 2 hours ago









                    JRE

                    18.2k43461




                    18.2k43461




















                        up vote
                        0
                        down vote













                        Remove what you have on the output side of U3 at the moment. Connect pin 3 of U3 to ground and pull up pin 4 to VCC with something like a 100k resistor. Add a capacitor between pin 4 and VCC if the output needs smoothing.



                        You are trying to use npn transistors as high side switches. This is not something you should try to do as a novice.






                        share|improve this answer
























                          up vote
                          0
                          down vote













                          Remove what you have on the output side of U3 at the moment. Connect pin 3 of U3 to ground and pull up pin 4 to VCC with something like a 100k resistor. Add a capacitor between pin 4 and VCC if the output needs smoothing.



                          You are trying to use npn transistors as high side switches. This is not something you should try to do as a novice.






                          share|improve this answer






















                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            Remove what you have on the output side of U3 at the moment. Connect pin 3 of U3 to ground and pull up pin 4 to VCC with something like a 100k resistor. Add a capacitor between pin 4 and VCC if the output needs smoothing.



                            You are trying to use npn transistors as high side switches. This is not something you should try to do as a novice.






                            share|improve this answer












                            Remove what you have on the output side of U3 at the moment. Connect pin 3 of U3 to ground and pull up pin 4 to VCC with something like a 100k resistor. Add a capacitor between pin 4 and VCC if the output needs smoothing.



                            You are trying to use npn transistors as high side switches. This is not something you should try to do as a novice.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered 2 hours ago









                            RoyC

                            4,91431431




                            4,91431431




















                                Ghazanfar Javed is a new contributor. Be nice, and check out our Code of Conduct.









                                 

                                draft saved


                                draft discarded


















                                Ghazanfar Javed is a new contributor. Be nice, and check out our Code of Conduct.












                                Ghazanfar Javed is a new contributor. Be nice, and check out our Code of Conduct.











                                Ghazanfar Javed is a new contributor. Be nice, and check out our Code of Conduct.













                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2felectronics.stackexchange.com%2fquestions%2f396825%2fhaving-trouble-in-gpio-output-220-v-to-5-v-signal%23new-answer', 'question_page');

                                );

                                Post as a guest













































































                                Comments

                                Popular posts from this blog

                                What does second last employer means? [closed]

                                List of Gilmore Girls characters

                                Confectionery