Jump to content

Reed switch and OrangePi Zero (H2)


Matthai

Recommended Posts

I would like to connect reed switch to OrangePi Zero with H2 processor.

 

Can I use this?

https://github.com/duxingkei33/orangepi_PC_gpio_pyH3

 

Under examples, there are two examples I think I could use:

- read_button.py

- read_key_PG7.py

 

I have two questions.

1. Where is the pinout?

 

read_button.py has this code:

led = connector.gpio0p0 # This is the same as port.PH2
button = connector.gpio3p40

 

read_key_PG7.py has this code:

led = connector.gpio1p38 # This is the same as port.PH2
button = connector.gpio1p40 #CHOW

Which pins are which, where to get the picture of GPIO pins? (And why LED's connector ID's are different in the above examples)?

 

Here is one example of pinouts, but the names are different:

http://codelectron.com/blink-leds-using-orange-pi-zero-gpio-and-python/

 

 

2. Where to connect reed switch? One is one of the GPIO pins, and other? GND, 5V, 3.3V? And - I assume I do not to use a resistor? Or I am wrong?

 

Link to comment
Share on other sites

Sorry, I do not understand.

 

As I understand, this:

button = connector.gpio1p40 #CHOW

 

This means, that button is connected to gpio1p40. In your link, I cannot find which pin that would be. I mean physically, where to connect reed switch, on which pins exactly?

Link to comment
Share on other sites

35 minutes ago, Matthai said:

This means, that button is connected to gpio1p40.

Don't use that kind of nomenclature, this is board related, and we don't even know which board it is. Please, use plain GPIO SoC nomenclature, such as "button = port.PA10" which is easy to find on schematic, here PA10 = pin 26.

Link to comment
Share on other sites

OK, I see. I just got confused by gpio1p40 notation. Thanks for clarification.

 

I assume I can connect switch to 3.3V or GND, it will just read the different state (once open, the other closed). But what about resistor? 4.7 kOhm together with internal pullup resistor seems OK? I mean, it is not necessary, but if GPIO pin is accidentally set to output, then I assume it will protect the board from frying? Or I am wrong?

Link to comment
Share on other sites

4 minutes ago, Matthai said:

I assume I can connect switch to 3.3V or GND,

Normally, switches are pulling down to GND, and a pullup resistor when switch are opened.

 

4 minutes ago, Matthai said:

but if GPIO pin is accidentally set to output, then I assume it will protect the board from frying?

If you want to prevent such scenario, you need to add a low value resistor in serie with the switch.

If you confident that you software doesn't do any of such accidental output, no need for such protection.

Link to comment
Share on other sites

OK, I see. So I connect reed switch to GND and GPIO pin and enable internal resistor:
 

gpio.setcfg(button, gpio.INPUT)
gpio.pullup(button, gpio.PULLUP) # enable internal pullup resistor

 

5 hours ago, martinayotte said:

Normally, switches are pulling down to GND, and a pullup resistor when switch are opened.

 

If you want to prevent such scenario, you need to add a low value resistor in serie with the switch.

If you confident that you software doesn't do any of such accidental output, no need for such protection.

I am confident, but what is happening when device is booting? Is it possible, that device goes to some weird state and GPIO pin set to output during device init?

By low value you mean 1k, more, less?

Link to comment
Share on other sites

3 minutes ago, Matthai said:

Is it possible, that device goes to some weird state

Normally, SoC are leaving GPIO floating until they are explicitly enabled as output.

3 minutes ago, Matthai said:

By low value you mean 1k, more, less?

Even less, just to avoid short and high current, something like 100ohms.

Link to comment
Share on other sites

OK, just an information for others, how I solved this:

 

1. Install Armbian (on OrangePi Zero H2+)

 

2. Install Python:

sudo apt install python python-pip python-setuptools python-dev

 

3. Install pyA20:
 

sudo pip install pyA20

 

4. Install GPIO:
 

git clone https://github.com/nvl1109/orangepi_zero_gpio
cd orangepi_zero_gpio/
sudo python setup.py install
cd ..

5. Create new Python app:

nano reed.py

 

With the content:

#!/usr/bin/env python

import os
import sys

if not os.getegid() == 0:
    sys.exit('Script must be run as root')

from time import sleep
from pyA20.gpio import gpio
from pyA20.gpio import port

button = port.PA10

gpio.init()

gpio.setcfg(button, gpio.INPUT)

"""Enable pullup resistor"""
gpio.pullup(button, gpio.PULLUP)
#gpio.pullup(button, gpio.PULLDOWN)     # Optionally you can use pull-down resistor

try:
    print ("Press CTRL+C to exit")
    while True:
        state = gpio.input(button)      # Read button state
        if (state == 1):
            print ("The door is OPENED.")
        else:
            print ("The door is CLOSED.")
        sleep(0.5)

except KeyboardInterrupt:
    print ("Goodbye.")

 

6. Connect reed switch:

- one wire to GND

- the other wire to GPIO 26 (PA10)

 

7. Run the app:

sudo python reed.py

 

The output:
 

The door is OPENED.
The door is OPENED.
The door is OPENED.
The door is OPENED.
The door is OPENED.
The door is CLOSED.
The door is CLOSED.
The door is CLOSED.
The door is CLOSED.
The door is OPENED.
The door is OPENED.

 

Now... the web app. But this is another story. :)

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

Terms of Use - Privacy Policy - Guidelines