Articles Hierarchy

Articles Home » RPI » RPI I2C

RPI I2C

RPI & BLINKA
to WEB SERVER
I2C device: ADC: ADS1115
Server-Side
I2C device: Arduino

SMBus


RPI & BLINKA


never ever used the RPI hardware I/O pins
( ok, i connected a fan on 5V there )
but no tinkering, use of hats...

why? i come from arduino ... ESP
and then started RPI,
so all electronic use of a ( not really cheap ) LINUX PC i avoided, as not needed.
also the RPI GPIO header is 3v3 and misses A-INs
so i choose always to use Arduino and send the data via USB.

but as i had a idea about a special I2C connection
and possibly do it with Circuit Python
but with a long term need of a Ethernet interface for this project
i changed my mind and try using a old ( model 2 start 02/2015) Raspberry Pi 2 Model B Rev 1.1


start reading adafruit circuit python and use of RPI boards in 2 ways:
++ loading circuit python directly on RPI hardware "standalone"
but only for RPI4 and zero ( incl W and 2W ) but not RPI3 or RPI2 ( as no USB OTG )
++ use RPI OS and BLINKA
( that i want try now)
to get the whole history story see Keynote: The Story Behind Blinka - Limor Fried

anyhow seems newest OS "bullseye" is required.


while the new setup i run into some problems..
- - - update break
- - - power low warning ( red led missing )
- - - vnc hangs
( possibly not a good idea to use that old board, toss it ? )


"BLINKA" install ( on RPI2B )
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install python3-pip
sudo pip3 install --upgrade setuptools
cd ~
sudo pip3 install --upgrade adafruit-python-shell
wget https://raw.githubusercontent.com/adafruit/Raspberry-Pi-Installer-Scripts/master/raspi-blinka.py
sudo python3 raspi-blinka.py



ls /dev/i2c* /dev/spi*
sudo nano /boot/config.txt add line
dtoverlay=spi1-3cs
and reboot
pi@RPI2:~ $ ls /dev/i2c* /dev/spi*
/dev/i2c-1 /dev/i2c-2 /dev/spidev0.0 /dev/spidev0.1 /dev/spidev1.0 /dev/spidev1.1 /dev/spidev1.2

and try a test program

if work via terminal:
nano blinkatest.py
python blinkatest.py

also
python
>>>import board
>>>help ("board")
>>>quit()
$

try from terminal with:
sudo i2cdetect -y 1
to find a I2C device connection
and from python
i2c = busio.I2C(board.SCL, board.SDA) # SCL1 GPIO3 pin 5 // SDA1 GPIO2 pin 3 // 3v3 pin 1 // GND pin 6
print("__ I2C devices found: ", [hex(i) for i in i2c.scan()])




working-with-i2c-devices

list of libs from bundle here


to WEB SERVER


side step: sounds complicated but i will stick with my old way to show data (local) online:

- a python service will get data and save it as file,
- a python web server will read that file and show its content ( HTML auto-refesh ON ),
++ where file is at the RAM DISK ( to protect the RPI SD card )


but first i have to dig into Flask again, see quickstart

the preinstalled Flask version on RPI is still 1.1.2 ( but now with python 3.9.2 )
( for what is it installed at all? an old Raspberry Pi : Python tutorial ? )

can try:
sudo pip3 install -U Flask ?-U means Upgrade ?
Successfully installed:
Flask-2.1.1
Jinja2-3.1.1
MarkupSafe-2.1.1
Werkzeug-2.1.1
click-8.1.2
importlib-metadata-4.11.3
itsdangerous-2.1.2
zipp-3.8.0



while the minimal start (of a 'hello world') with 5 lines in a app.py is easy,
better take the next step into 'templates' and make a structure like:
app.py
/templates/index.html
/templates/header.html
/templates/footer.html
/static/images/favicon.ico
/static/assets/css/main.css

and if want PORT=80 ( if this is the only web server on that RPI )
must start as
sudo python app.py


as example this index.html thanks to the Jinja2 template engine

and with next step, where a new python service:
* get sensor data
* format to readable info text
* for info add a record counter
* write to RAM DISK file
** every 10 sec


code
-- status web server and data service but no data collection ( dummy counter data / 10 sec each )
-- pls copy each .desktop file to your desktop ( or to ~/.config/autostart )
--- ( after adjusting your correct path.. in 'start' and 'start.desktop' )

tested from PC by browser 'http://192.168.1.102/'
Firefox/Brave/Edge/ ok
Chrome (100) ( no show, WAITING FOR PLAY.GOOGLE.COM ) but with inspect not find a issue?
how to allow the auto update there?


I2C device: ADC: ADS1115


now the most reasonable add on to the RPI GPIO
would be a ADC, linked by I2C
here i use the ADS1115
++ 16 bit
++ 4 channel ( or 2 differential )
++ programmable gain : 2/3x to 16x
++ 860 samples /s
++ I2C address default (0x48) ( range: 0x48-0x4B by connect ADDR pin to GND || VDD || SDA || SCL )

tried to make photos with 2 phones, macro / lighting & flash / auto focus / all apps suck


from ( thanks to ) Adafruit a connection diagram:



what is wrong with me... ( besides getting old )
again play some RPI software / write here in the BLOG / often several topics parallel / easy and fun
BUT to pull that 4 wires and get it tested somehow i am blocked? ( now for 3 weeks already )
sure have other active projects like our new view point

rip off the garage roof, make a new roof 3 m higher as a terrace with coffee table..
possibly a good place to work via laptop ( remotely to RPI ... ) but if there is sunshine can't read the screen.


lucky i have some female-female wires, so can connnect the ADS1115 with the RPI2 header without breadboard.


see the cheap DIY housing / distance mounting??
warning: bus wiring is a science / know how / for its own
-1- the length of the ADS breakout board cable always results in a speed limit, but not have shorter ones avail.
-2- also the question if twisted wires are required here?
-3- the RPI I2C pins do have pull-ups installed internally, more required, like on the peripheral board? also depends on speed and distance...


like for above -2- question twisted wires?
wiki/Twisted_pair
you know what i am talking about? why untwisted wires are bad?
in a untwisted signal wire ( or better a loop by a wire and its ground wire ) a external electromagnetic interference can happen,
and be experienced as NOISE.
when you then twist that 2 wires ( signal and ground wire ( or 2wire differential signal )) that interference cancels itself out
or reduces the area by the 2 wires what can catch the interference.
example the ethernet cable uses twisted pairs or twisted and shielded ( like cat5e )
the number 'twists per meter' is also called 'pitch'.

but still, as higher the signal frequency, as shorter the cable must be.
I2C was never meant to be a bus link between 2 computers ( like ethernet or RS485 where we talk 1 .. 300m )
it was for connect boards inside the same box / cabinet.




looks good already!

following see measured data where i have connected
++ A0 to RPI GND
++ A1 to RPI 3V3
++ A2 A3 open / floating?


with the data-service ( to RAM-DISK ) for 4 channels
and A0 connected to GND and A1 to 3V3

also on web-server

and this code

now this makes a 4 channel measurement ( 5 line output text to file ) every 10 seconds ( by delay ), what a waste of cpu capacity.
some mods...
first FORMATTING: make it one clean CSV line:
data:
1, -0.001, 3.298, 0.567, 0.565,

2, -0.001, 3.298, 0.566, 0.564,

3, -0.001, 3.299, 0.567, 0.564,

4, -0.001, 3.299, 0.566, 0.564,

second change the program / timing structure:
* * make with a new short delay timer ( sample rate ) many measurements and append the lines to a temp file
* * after 320 samples ? for a mini scope ? overwrite the file


this needs 15sec,
when i tried a version where i open file once, write 320 lines, close/copy needed 14sec ( still poor 91 smps )
( library default timer down to 125 smps if SINGLE ( single shot mode ),
so by sample only 'A0' and change mode to CONTINUOUS see 3 320 lines files per sec ( theoretically 860 smps) )
cat /usr/local/lib/python3.9/dist-packages/adafruit_ads1x15/ads1x15.py
( cat /usr/local/lib/python3.9/dist-packages/adafruit_ads1x15/ads1115.py )

code

the 320 lines ( 4 channels ) .csv file is printed by webserver..
a graph version is ( like code a .SVG file to show ) pending LOL

Server-Side


we take a other side step from above web server side step
as i see https://www.instructables.com/From-Data-to-Graph-a-Web-Jorney-With-Flask-and-SQL/
so even we use here a old RPI2,
a little server enhancement like with SQLite should be possible.
sudo apt-get install sqlite3
sudo apt-get install sqlitebrowser
sudo apt-get install python3-matplotlib

+ + i like his teaching SQLite3
and the power point style graphs what show the app structure.. are excellent!
also the result with hist plot and gage is impressive ( using matplotlib and a justgage.js tool )
- - but that part i would not use.

so in the following part about Arduino ( as I2C Ain 4 channel )
i show the current reading in webserver ( as one CSV line )
but also add the readings into a historic database ( /run/shm/hist.db table: Hist_data )
for further use.



I2C device: Arduino


lets say we want to use a RPI as I2C Master and a Arduino as I2C Slave
!! new wording should use now like:
“Controller / Peripheral”
oshwa.org

problem is: RPI is 3v3 and Arduino is 5v ( signal level )
we need level shifting:
i have a:

CJMCU_0108_bidirectional_level_shifter
info:
https://www.ti.com/lit/ds/symlink/txs0108e.pdf?ts=1649585369005
https://www.adafruit.com/product/395
https://www.artekit.eu/using-logic-level-converters/
https://www.youtube.com/watch?v=f7aySy_0URE
https://www.diymachines.co.uk/tsx0108e-logic-level-converter
Arduino peripheral example:
https://cujo.com/hardware-hacking-101-e01-i2c-sniffing/

download arduino-nightly-windows.zip on 28.4.2022 unzip to win 10 PC
a '1.8.19 update' called '1.8.20 2022/04/25'
and uploaded the above code into my old Ardunio Leonardo
and adjust so i see in 'monitor' that it is working.. ( print "loop" )
I2C add is '0x30'



now a even better connection should a opto isolated I2C bus,
( where even the GND not is connected )
breakout
learning
datashheet
i not find such a board here, but the chip only? shopee


SMBus


while with linux comes a SMBus installed,
there are newer python libs smbus2
pypi.org/project/smbus2/
pip3 install smbus2