2uk
Posts: 9
Joined: Sun Apr 07, 2019 5:23 pm

26 GPIO to drive 26 Optocoplers

Mon Apr 08, 2019 6:52 pm

Hello to all
I have read that the PI 3 B+ has 26 GPIO and that they can be reprogrammed (is that true?)
than my question will be Can I use all 26 GPIOs
(GPIO 2,3,4,17,27,22,10,9,11,5,6,13,19,26,21,20,16,12,7,8,25,24,23,18,15,14)
(Pins 3,5,7,11,13,15,19,21,23,29,31,33,35,37,8,10,12,16,18,22,24,26,32,36,38,40) .
I have read that GPIO-2,3,4,14,15 are High during Boot, but after can operate normally.

I wold like to drive as many Optocuplers as possible with 1 board.

Thank You in advance.
Mihai L.

User avatar
Burngate
Posts: 5884
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK Tralfamadore
Contact: Website

Re: 26 GPIO to drive 26 Optocoplers

Tue Apr 09, 2019 9:17 am

Short answer: yes.

Slightly longer answer: at power-on, GPIOs 0 - 8 are pulled up, with the rest pulled down, and all set as inputs.
Pretty soon, many of them change to their ALT functions - I2C, UART, etc.
But you can change them to what you want when you want.

Since you say you want to drive optocouplers rather than sense them, so outputs, their pulls have no significance.
But as inputs, GPIOs 2 & 3 have quite strong external pull-ups, which might affect what you could do.

GPIOs 0 & 1 (which you don't mention) appear on pins 27 & 28, to detect HATs. They're also available for general use, once it's found no HATs
As a matter of interest, one of the DPI settings (888) uses all of GPIOs 4 - 27 to drive a DPI screen.

LTolledo
Posts: 1515
Joined: Sat Mar 17, 2018 7:29 am

Re: 26 GPIO to drive 26 Optocoplers

Tue Apr 09, 2019 10:02 am

if it were me, I'd use 2 x MCP23017 on the I2C line (or 2 x MCP23S17 on the SPI line) to drive the optocouplers (can have max 32 outputs)
that way I can free RPi's GPIO for other use.

if wanted more, just add additional MCP23017 (up to max 8 [8 x 16 = 128]),

but then again 128 optocouplers maybe a bit overboard for your use.....

there exist a HAT that has 2 x MCP23017 on it. just search the internet using your favorite search engine...
"Don't come to me with 'issues' for I don't know how to deal with those
Come to me with 'problems' and I'll help you find solutions"

Some people be like:
"Help me! Am drowning! But dont you dare touch me nor come near me!"

User avatar
NGC6543
Posts: 75
Joined: Thu Feb 28, 2019 7:30 pm
Location: Lancashire, UK

Re: 26 GPIO to drive 26 Optocoplers

Tue Apr 09, 2019 1:38 pm

I agree with using port expanders on the SPI or I2C bus for that many outputs. That gets away from the current limit on the GPIO pins as well.

User avatar
omegaman477
Posts: 145
Joined: Tue Feb 28, 2017 1:13 pm
Location: Sydney, Australia

Re: 26 GPIO to drive 26 Optocoplers

Tue Apr 09, 2019 2:20 pm

2uk wrote:
Mon Apr 08, 2019 6:52 pm
Hello to all
I have read that the PI 3 B+ has 26 GPIO and that they can be reprogrammed (is that true?)
than my question will be Can I use all 26 GPIOs
(GPIO 2,3,4,17,27,22,10,9,11,5,6,13,19,26,21,20,16,12,7,8,25,24,23,18,15,14)
(Pins 3,5,7,11,13,15,19,21,23,29,31,33,35,37,8,10,12,16,18,22,24,26,32,36,38,40) .
I have read that GPIO-2,3,4,14,15 are High during Boot, but after can operate normally.

I wold like to drive as many Optocuplers as possible with 1 board.

Thank You in advance.
Mihai L.
Not all the GPIO pins are available, especially if you want to use I2C, SPI, Serial or another I/O hat.

Do you want to send control signals (output) or recieve (input). Opto isolators are single direction.

Depending on the scan rate you need to drive or sense these opto's, I agree with the others, I2C connected I/O expanders is the way to go. I have used these ready to roll boards, they also do an output version. https://www.ereshop.com/shop/index.php? ... cts_id=777

Also this https://www.banggood.com/3pcs-CJMCU-857 ... rehouse=CN

Coupled to this https://www.banggood.com/8-Channel-24V- ... rehouse=CN
..the only thing worse than a stupid question is a question not asked.

2uk
Posts: 9
Joined: Sun Apr 07, 2019 5:23 pm

Re: 26 GPIO to drive 26 Optocoplers

Wed Apr 10, 2019 3:08 pm

Wow , thank you very much for all the answers , sorry if I missed information about this project .

so I want to use them all as outputs, I will drive Optocuplers to switch the status from my house electronics :) (windows and doors to open A/C , so I wold use the PI with an Amazon Alexa software and connection over Internet, than giving commands from a telephone or another Echo Device.

I only need to drive maxim of DC 12V and Optocuplers PC817 are rated at DC30V
and being electrically isolated I dont see the need to drive a Relay.
Schematic_example_Sheet-1_20190410164647-page-001.jpg
Schematic_example_Sheet-1_20190410164647-page-001.jpg (137.93 KiB) Viewed 1143 times
the LED is optional the resistor I am not shore, I am in need of about 16 Outputs to Optocuplers. I will drive with the Optocupler the switch from the remote controls (windows , doors)if that works I wold like to apply it to my auto too ( in somer to open windows and start the fan , in winter start heating seats and windscreen.

LTolledo thank you for the info I will consider it , if I will be in need for more Outputs , tho I dont know if I can use it with an Echo software .

Burngate in my schematic they are noted as
DNC I did not know what that is, so I cold possibly drive (output) 28 GPIOs ?

sincere thanks to all for all the information
Mihai L.

User avatar
Burngate
Posts: 5884
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK Tralfamadore
Contact: Website

Re: 26 GPIO to drive 26 Optocoplers

Wed Apr 10, 2019 5:48 pm

2uk wrote:
Wed Apr 10, 2019 3:08 pm
the LED is optional the resistor I am not shore
The resistor is necessary, to control the current through the opto's LED

2uk
Posts: 9
Joined: Sun Apr 07, 2019 5:23 pm

Re: 26 GPIO to drive 26 Optocoplers

Wed Apr 10, 2019 6:24 pm

Burngate wrote:
Wed Apr 10, 2019 5:48 pm
2uk wrote:
Wed Apr 10, 2019 3:08 pm
the LED is optional the resistor I am not shore
The resistor is necessary, to control the current through the opto's LED
I am not sure how to calculate the resistor
so the PC 817 is rated 6V at 50mA http://html.alldatasheet.com/html-pdf/4 ... PC817.html
tha PI suplys 5v at 300mA I think
doing the Ohm Law is R=(Voltage Supplied - Voltage Needed) / 50mA
R=(5-6)/0.05=-20
I am doing it wrong or ?

User avatar
rpdom
Posts: 14416
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: 26 GPIO to drive 26 Optocoplers

Wed Apr 10, 2019 6:39 pm

2uk wrote:
Wed Apr 10, 2019 6:24 pm
Burngate wrote:
Wed Apr 10, 2019 5:48 pm
2uk wrote:
Wed Apr 10, 2019 3:08 pm
the LED is optional the resistor I am not shore
The resistor is necessary, to control the current through the opto's LED
I am not sure how to calculate the resistor
so the PC 817 is rated 6V at 50mA http://html.alldatasheet.com/html-pdf/4 ... PC817.html
tha PI suplys 5v at 300mA I think
doing the Ohm Law is R=(Voltage Supplied - Voltage Needed) / 50mA
R=(5-6)/0.05=-20
I am doing it wrong or ?
Forward voltage on the INPUT side is 1.2-1.4V.
Maximum current is 50mA, but the LED will work on much less than that. 10mA should be enough.

3.3V - 1.4V = 1.9V

1.9V / 0.01A = 190 Ohms. Closest standard value is 180R or 220R. I'd go for the higher value to reduce the current.

User avatar
NGC6543
Posts: 75
Joined: Thu Feb 28, 2019 7:30 pm
Location: Lancashire, UK

Re: 26 GPIO to drive 26 Optocoplers

Wed Apr 10, 2019 6:50 pm

First, the optocouplers will only give a very small current through the output. Around 1.2 times the current you give the input LED, from the graph on the PC817 datasheet. It varies though.

As to your use of Ohm's law, this is how it would be done for that optocoupler:

Decide what current you want to drive the optocoupler's LED at. Let's say 5mA to give it a reasonable current. The Vf versus If graph shows about 1.0V Vf at 5mA. So that's what voltage the LED will 'want' if you feed it 5mA.

The Pi's GPIOs give 3.3V (not 5V). So, your resistor needs to drop the remaining voltage: 3.3V - 1.0V = 2.3V.

So, the resistor will have 2.3V across it with the 5mA current. Ohm's law gives R=V/I = 2.3/0.005 = 460 ohms. A near standard value is 470 ohms - that will do.

Edit to add: Or, rpdom's (who clearly types faster than me) version above with a higher current is valid too :)
Last edited by NGC6543 on Wed Apr 10, 2019 6:57 pm, edited 3 times in total.

LTolledo
Posts: 1515
Joined: Sat Mar 17, 2018 7:29 am

Re: 26 GPIO to drive 26 Optocoplers

Wed Apr 10, 2019 6:52 pm

It seems that Echo software uses wiringpi, and wiringpi supports MCP23017 so it may be possible.

http://wiringpi.com/extensions/i2c-mcp23008-mcp23017/
"Don't come to me with 'issues' for I don't know how to deal with those
Come to me with 'problems' and I'll help you find solutions"

Some people be like:
"Help me! Am drowning! But dont you dare touch me nor come near me!"

2uk
Posts: 9
Joined: Sun Apr 07, 2019 5:23 pm

Re: 26 GPIO to drive 26 Optocoplers

Fri Apr 12, 2019 2:05 pm

LTolledo wrote:
Wed Apr 10, 2019 6:52 pm
It seems that Echo software uses wiringpi, and wiringpi supports MCP23017 so it may be possible.

http://wiringpi.com/extensions/i2c-mcp23008-mcp23017/
nice to know thank you,
than I have the next question "how to I connect 2 of them ? " on internet I find only how to connect 1Image
thx :) I am thinking to use ULN2803 if I will have problems with the curent or voltage out of GPIOs

that is doable yes ?

thank you
Mihai L.
Attachments
Schematic.jpg
Schematic.jpg (89.95 KiB) Viewed 990 times

2uk
Posts: 9
Joined: Sun Apr 07, 2019 5:23 pm

Re: 26 GPIO to drive 26 Optocoplers

Sat Apr 13, 2019 11:50 am

2uk wrote:
Fri Apr 12, 2019 2:05 pm
LTolledo wrote:
Wed Apr 10, 2019 6:52 pm
It seems that Echo software uses wiringpi, and wiringpi supports MCP23017 so it may be possible.

http://wiringpi.com/extensions/i2c-mcp23008-mcp23017/
nice to know thank you,
than I have the next question "how to I connect 2 of them ? " on internet I find only how to connect 1Image
thx :) I am thinking to use ULN2803 if I will have problems with the curent or voltage out of GPIOs

that is doable yes ?

thank you
Mihai L.
Pin 9 (VDD) is connected to 3.3V
Pin 10 (VSS) is connected to Ground
Pin 12 (SCL) is connected to Pin 5 on the Pi GPIO
Pin 13 (SDA) is connected to Pin 3 on the Pi GPIO
Pin 18 (Reset) should be set high for normal operation so we connect this to 3.3V
Pins 15, 16 & 17 (A0-A2) determine the number assigned to this device. We are only using one device so we will give it a binary zero by setting all three of these pins to 0 (ground)

so if I want 2 of them or more I will need to re order A0 A1 A2 .
for - 1x MCP23017 all to Ground
for - 2x MCP23017 I need to set one (A0 A1 ground) and one (A1 A2 Ground) ?
Image

is it correct ?
Please help , thank you
Mihai L.
Attachments
Schematic2.jpg
Schematic2.jpg (112.34 KiB) Viewed 950 times

2uk
Posts: 9
Joined: Sun Apr 07, 2019 5:23 pm

Re: 26 GPIO to drive 26 Optocoplers

Sun Apr 21, 2019 2:40 am

hi all
sorry for my absence , Just got a lot on my head and since I do this as Hobby needed to wait,
so I came up with this schematic Schematic3.jpg Image

I have 3 problems >

Problem 1 - can I connect the Resistor on catode or anode ? as I have seen on a few examples on Internet it is not important were , it only need to be there !

Problem 2 - since I want to make a extreme small board I`am thinking to reduce the number of components
Or I need to do it as here Optocoupler.png Image

Problem 3 - I have to give a binary number of 0, 1, 2 , with 0 being A0,A1,A2 all to Ground (that is correct , yes ?) so: binary-page-001.jpg Image

Please correct me were I am wrong
thank you
Mihai
Attachments
Optocoupler.png
Optocoupler.png (7.43 KiB) Viewed 846 times
binary-page-001.jpg
binary-page-001.jpg (55.5 KiB) Viewed 846 times
Schematic3.jpg
Schematic3.jpg (60.34 KiB) Viewed 846 times

LTolledo
Posts: 1515
Joined: Sat Mar 17, 2018 7:29 am

Re: 26 GPIO to drive 26 Optocoplers

Sun Apr 21, 2019 6:01 am

Problem 1: connect to either anode or cathode, my preference is usually on the anode side. but may change during the PCB layout.

Problem 2: if you still want to use a relay, then the board space will get bigger. You may group your outputs depending on the load you want to connect.
low amperage module (optocoupler-transistor array combo) for low ampere loads (less than 200mA), while another group for higher loads with relays connected to output of the low amperage modules. You may also have a "dry contact" module comprising of just the optocouplers (optocoupler outputs not connected to any Vcc and ground on the board).

Problem 3: If this is for the MCP23017 then yes. for the first MCP23017 you may have A0, A1, and A2 all connected to ground. For the second MCP23017, the connection will be A0 --> +3.3v, A1 and A2 to ground. In your binary table A2 and A0 are reversed.

The MCP23017 data sheet states that the maximum current per pin is 25mA but the max the current that can pass thru the MCP23017 is 125mA (Vdd pin), so that means max 5 pins sourcing or sinking 25mA. However with 5mA recommended current to flow thru each optocoupler, you can have all pins sinking or sourcing current at the same time, the total current used by the chip will only be below 90mA, so you wont fry the MCP23017.

somebody once told me ".....design with the worst possible scenario in mind, .....hope for the best possible outcome....."
"Don't come to me with 'issues' for I don't know how to deal with those
Come to me with 'problems' and I'll help you find solutions"

Some people be like:
"Help me! Am drowning! But dont you dare touch me nor come near me!"

2uk
Posts: 9
Joined: Sun Apr 07, 2019 5:23 pm

Re: 26 GPIO to drive 26 Optocoplers

Sun Apr 21, 2019 8:26 am

LTolledo wrote:
Sun Apr 21, 2019 6:01 am
Problem 1: connect to either anode or cathode, my preference is usually on the anode side. but may change during the PCB layout.

Problem 2: if you still want to use a relay, then the board space will get bigger. You may group your outputs depending on the load you want to connect.
low amperage module (optocoupler-transistor array combo) for low ampere loads (less than 200mA), while another group for higher loads with relays connected to output of the low amperage modules. You may also have a "dry contact" module comprising of just the optocouplers (optocoupler outputs not connected to any Vcc and ground on the board).

Problem 3: If this is for the MCP23017 then yes. for the first MCP23017 you may have A0, A1, and A2 all connected to ground. For the second MCP23017, the connection will be A0 --> +3.3v, A1 and A2 to ground. In your binary table A2 and A0 are reversed.

The MCP23017 data sheet states that the maximum current per pin is 25mA but the max the current that can pass thru the MCP23017 is 125mA (Vdd pin), so that means max 5 pins sourcing or sinking 25mA. However with 5mA recommended current to flow thru each optocoupler, you can have all pins sinking or sourcing current at the same time, the total current used by the chip will only be below 90mA, so you wont fry the MCP23017.

somebody once told me ".....design with the worst possible scenario in mind, .....hope for the best possible outcome....."
Thank you for the fast Response, at this Point i think i have all i need on the Hardware site, going forward with the PCB Design and will come back for an PCB control ( if some one will be so kind, last will be the Software.

Thank you very much to all
See ya all in a bit :)
Mihai

2uk
Posts: 9
Joined: Sun Apr 07, 2019 5:23 pm

Re: 26 GPIO to drive 26 Optocoplers

Sun May 05, 2019 7:14 pm

So hi all ,
Here I am again , is not easy to find the perfect components and the perfect order for them , I am doing it here https://easyeda.com/editor#id=|682545b6 ... 508d5e1622 if some one want to help me a little I will appreciate , I an doing it on 8x5 cm or so , because all PCB Orders are like 10x10 cm for a good price and I am thinking to introduce later 2 on the same board as for components I will be using:
  • Resistors SMD on the dimensions 1206 *will need to calculate the resistivity*
  • Optocuplors SMD PC817
  • Relays SMD HFD4/05-S -5V rated at 220V 2A
  • SMD MCP23017-E/SO
  • SMD ULN2804 powered by an USB type B
  • I am still searching for some ultra small switches for the A0 A1 A2
  • a Diode I think 1n4222 to stop the fly back current from relays
thank you
Mihai L.

2uk
Posts: 9
Joined: Sun Apr 07, 2019 5:23 pm

Re: 26 GPIO to drive 26 Optocoplers

Sun May 26, 2019 7:44 pm

sorry for the late response

here is the final product , I wood like if some one corrects it (if I did something wrong) thank youImage

the ling to file is here https://easyeda.com/editor#id=|aba6b55d ... 5d090882ce
thank you for all the help guys
Attachments
PCB_20190526212747-page-002.jpg
PCB_20190526212747-page-002.jpg (128.45 KiB) Viewed 429 times
PCB_20190526212653-page-001.jpg
PCB_20190526212653-page-001.jpg (118.68 KiB) Viewed 429 times

Return to “Automation, sensing and robotics”