element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Off the Shelf
  • Challenges & Projects
  • Project14
  • Off the Shelf
  • More
  • Cancel
Off the Shelf
Blog Arduino Nano as Raspberry Pi IO I2C expander PART 4 - humbling persistence, scope creep, Python Voodoo, and over the hump
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Off the Shelf to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: robogary
  • Date Created: 28 Sep 2021 1:45 AM Date Created
  • Views 740 views
  • Likes 1 like
  • Comments 0 comments
  • offtheshelfch
Related
Recommended

Arduino Nano as Raspberry Pi IO I2C expander PART 4 - humbling persistence, scope creep, Python Voodoo, and over the hump

robogary
robogary
28 Sep 2021

The next update will be the final project completed !!

Test code of 5 bytes of data from the Arduino to the Raspberry Pi is working and being melded with functionality for meeting the final project goal + some scope creep that just fit in too well.

 

Thank you to these resources whose examples helped me the most to figure out the code:

https://oscarliang.com/raspberry-pi-arduino-connected-i2c/

https://roboticsbackend.com/raspberry-pi-master-arduino-slave-i2c-communication-with-wiringpi/

https://36projectsblog.wordpress.com/8-raspberry-pi-reading-i2c-sensors-using-python/

 

 

I still had to overcome my own stumblings - like reading the Arduino analog values over I2C, reconstructing the values to INT from bytes, then typing in an incorrect scale factor on the high byte. 

Added a feature to enable/disable the rotor main motor using the joystick push down switch. Unfortunately I forgot the switch N.O. contact was pulled down to zero rather than a pulled up to 5V.

 

This is the working Raspberry Pi Python code to read 5 bytes from the Arduino Nano.

I kept some of the code that didnt work in the python file , commented out, as a reminder of how NOT to do it.

 

The python code looks small, but it involves alot of suffering, mental anguish, and mindless determination  :-) 

 

I almost pulled out a scope to start troubleshooting the I2C data, realizing a data analyzer really is the needed tool - I'll be on watch for a Road Test opportunity for one of those  :-)

 

import smbus

import time

# for RPI version 1, use "bus = smbus.SMBus(0)"

bus = smbus.SMBus(1)

# This is the address we setup in the Arduino Program

address = 0x08

IOarray=([ 0,0,0,0,0,0])

 

 

## def writeNumber(value):

##    bus.write_byte(address, value)

    # bus.write_byte_data(address, 0, value)

##    return -1

 

 

##def readNumber():

##    number = bus.read_byte(address)

    #number = bus.read_byte_data(address, 1)

##    return number

 

 

while True:

    time.sleep(2)

    JoystickData = bus.read_i2c_block_data(0x08, 0x00, 5)

    print (" JoystickData=", JoystickData)

    LeftRightJoystick=(JoystickData[0]+(JoystickData[1]*256))

    print (" LeftRightJoystick =", LeftRightJoystick)

    FwdBackJoystick=(JoystickData[2]+(JoystickData[3]*256))

    print (" FwdBackJoystick =", FwdBackJoystick)

    EnableServos = JoystickData[4]

    print (" EnableServos =", EnableServos)

 

 

    #the array line by byte doesnt work correctly. Shown as a warning to not do it this way

    #IOarray[0] = bus.read_byte_data(address,0x00) ## this does not work right to read multiple bytes

    #IOarray[1] = bus.read_byte_data(address,0x01) ##right hi byte

    #IOarray[2] = bus.read_byte_data(address,0x02) ##back  lo byte

    #IOarray[3] = bus.read_byte_data(address,0x03) ##fwd hi byte

    #IOarray[4] = bus.read_byte_data(address,0x04) ## constant 16

    #print ("IOArray[0]=", IOarray[0])

    #print ("IOArray[1]=", IOarray[1])

    #print ("IOArray[2]=", IOarray[2])

    #print ("IOArray[3]=", IOarray[3])

    #print ("IOArray[4]=", IOarray[4])

 

Up Next - the final python code, Arduino code, schematic, and an action video  of the working project - sorry Vin Diesel was not available to operate the joystick

  • Sign in to reply
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube