Hey,
I really need some help! I’ve bought the IO Expander module, I’ve followed your instructions and downloaded your examples as shown here - pimoroni-ioexpander · PyPI . My Raspberry Pi Zero W detects the module via "i2cdetect -y 1“ as it should be.
BUT
no matter what I do, Thonny gives me the following error:
%Run Feuchtigkeitssensor1.py
Traceback (most recent call last):
File “/home/Niklas/Feuchtigkeitssensor1.py”, line 4, in
import ioexpander as io
ModuleNotFoundError: No module named ‘ioexpander’
I’ve been searching for the ioexpander module script in your libraries for two weeks now, but I can’t find it! Without it every script I try stops right at the beginning. This is my first project coding in python3 (and coding at all). I’ve changed one of your examples a little, to read out my AZ Delivery Soil Moisture Sensor V1.2:
#!/usr/bin/env python3
import time
import ioexpander as io
print(“”"analog.py
Demonstrates handling a single analog input using IO Expander.
You should wire a rotary potentiometer to pin 12.
The pot should be connected to 3.3v power and ground.
Press Ctrl+C to exit.
“”")
ioe = io.IOE(i2c_addr=0x18)
ioe.set_adc_vref(5.0) # Input voltage of IO Expander, this is 3.3 on Breakout Garden // at my sensor it’s 5V.
ioe.set_mode(7, io.ADC) # I want to pin it to the ioexpander-pin no. 7
last_adc = 0.00 # don’t know what that means but I’ll leave it in.
while True:
adc = ioe.input(7)
adc = round(adc, 2) # why ‚2‘?
if adc != last_adc:
print("{:.2f}v".format(adc))
last_adc = adc
time.sleep(1.0 / 30)
thanks.
Well I know the library and I’ve downloaded it into an virtual environment (ve), but I still get the same error. This is what I did so far:
- Headless Debian Bullseye installation (Raspi OS Lite)
- Repairing SSH by clearing old SSH-adresses from Mac in sudo nano /Users/niklas/.ssh/known_hosts
- sudo raspi-config - Peripherals - activate i2c
- sudo apt update && sudo apt upgrade && sudo apt-get install i2c-tools && sudo apt install thonny && sudo apt install virtualenvwrapper python3-virtualenvwrapper // then check with i2cdetect -y 1 if the ioexpander is detected - yes (0x18)
- sudo nano ~/.bashrc
source /usr/share/virtualenvwrapper/virtualenvwrapper.sh
PY_ENV_DIR=~/Pimoroni/venv
if [ ! -f $PY_ENV_DIR/bin/activate ]; then
printf "Creating user Python environment in $PY_ENV_DIR, please wait...\n"
mkdir -p $PY_ENV_DIR
python3 -m venv --system-site-packages --prompt Pimoroni $PY_ENV_DIR
fi
printf " ↓ ↓ ↓ ↓ Hello, we've activated a Python venv for you. To exit, type \"deactivate\".\n"
source $PY_ENV_DIR/bin/activate
- sudo reboot # Raspi installs the virtual environment from the bash script and starts in the ve.
- Within the ve ( (Pimoroni) Niklas@HydroPi:~ $): sudo apt install python3-pip && pip install jedi && python3 -m pip install pimoroni-ioexpander
- Thonny: Try with an example in /home/Niklas/Pimoroni/venv/lib/python3.11/site-packages/ioexpander if everything works.
Output:
Traceback (most recent call last):
File “/home/Niklas/Pimoroni/venv/lib/python3.11/site-packages/ioexpander/devices.py”, line 3, in
import ioexpander
ModuleNotFoundError: No module named ‘ioexpander’
Ok, your way ahead of me with the Virtual Environment. I’ve been procrastinating and going out of my way to avoid having to use it.
Hopefully somebody smarter than me will chime in, and give you a hand. I have an ioexpander, I’ve only ever used it on a Pico though.