Translate I2C device driver for Arduino to Python for Raspberry pi

Translate I2C device driver for Arduino to Python for Raspberry pi

When I searched for a closet to see if there were any parts that could be used to make a robot, I bought a motor driver for Raspberry pi a long time ago, but only the library for Arduino was prepared and it was stored in the motor driver (Grove's I2C). Discovered Motor Driver V1.3). He decided to translate it in python for Raspberry pi because he was free on the make-up day.

What is I2C communication?

Since the motor driver this time communicates with the microcomputer using the communication method called I2C, I decided to start with that understanding.

I can't put it together well, so I'll leave the details to this site ([How to use I2C communication] [1]). Roughly speaking, it seems to communicate like this.

  1. After the communication is initialized, the master side sends the address of the slave to be communicated from now on.
  2. Send data after receiving a response from Slave
  3. Termination of communication

Understanding the program for Arduino

I somehow understood the I2C communication method, so I saw how it was written in the sample program for Arduino. (Excerpt of communication part only)

Wire.begin();
delayMicrosecond(10000);

Wire.beginTransmission(i2c_add);
Wire.write(0x82);
Wire.write(0x0a);
Wire.write(0x01);

Wire.endTransmission();

Roughly speaking, the first and second lines initialize the communication, and the fourth line sends the slave address. Data is sent on the 5th to 7th lines. It was like the end of communication on the 9th line.

Rewrite for Raspberry pi

Since it is troublesome to make I2C communication from scratch, I decided to use the communication library smbus for Raspberry pi. With this,

import smbus
i2c = smbus.SMBus(1)
i2c.write_i2c_block_data(i2c_add,0x82,[0x0a,0x01])

It fits in 3 lines like this.

The first line is for importing the library, the second line is for creating an instance for communication, and the Raspberry pi uses 1 for the I2C bus (I'm not sure), so this seems to be SMBus (1), and the third line is the communication instruction. Sentence.

Here, the reason why the arguments 0x82, [0x0a, 0x01] are divided into the front one and the back two is On the Arduino side, all three of 0x82, 0x0a, 0x01 written in Wire.write have been grouped as "data" above, but 0x82 is the address of the register in the slave, and 0x0a, 0x01 is that register. It was because the meaning was a little different from the data written in.

Summary

If you use smbus, you can rewrite communication in about three lines above, and the most important thing is

i2c.write_i2c_block_data(Slave address,Register address,[Data to write])

This sentence. This time I used this instruction because the data to be written is 1 byte or more, but if it is 1 byte

i2c.write_byte_data(Slave address,Register address,Data to write)

Use this, both addresses and data can be specified as int type. [1]:http://www.picfun.com/c15.html#:~:text=I2C%EF%BC%88Inter%2DIntegrated%20Circuit%EF%BC%89,%E3%82%92%E5%AE%9F%E7%8F%BE%20%E3%81%99%E3%82%8B%E6%96%B9%E5%BC%8F%E3%81%A7%E3%81%99%E3%80%82

Recommended Posts

Translate I2C device driver for Arduino to Python for Raspberry pi
Update Python for Raspberry Pi to 3.7 or later with pyenv
Run AWS IoT Device SDK for Python on Raspberry Pi
[Raspberry Pi] Changed Python default to Python3
How to use Raspberry Pi pie camera Python
Device driver to get CPU information for ARM
Serial communication between Raspberry pi --Arduino Uno (Python)
Connect to MySQL with Python on Raspberry Pi
From setting up Raspberry Pi to installing Python environment
How to use the Raspberry Pi relay module Python
Raspberry Pi + python + IoT device, environment construction procedure to start image processing and machine learning
I talked to Raspberry Pi
Access google spreadsheet using python on raspberry pi (for myself)
Introducing PyMySQL to raspberry pi3
Raspberry Pi + Python + OpenGL memo
raspberry pi 1 model b, python
~ Tips for beginners to Python ③ ~
2020 Arduino / Raspberry Pi / Python / Microcomputer C language learning ~ Recommended book ~
Introduction to Python For, While
Connect Raspberry Pi to Alibaba Cloud IoT Platform with Python
Control the motor with a motor driver using python on Raspberry Pi 3!
I want to run the Python GUI when starting Raspberry Pi
Use python on Raspberry Pi 3 to illuminate the LED (Hello World)
How to play music (wav / mp3) files on Raspberry Pi python
Port FreeRTOS to Raspberry Pi 4B
Use vl53l0x with Raspberry Pi (python)
Output from Raspberry Pi to Line
Power on / off Raspberry pi on Arduino
An introduction to Python for non-engineers
Handmade Alexa Festival! Serverless, Invitation to IoT + Voice. Raspberry Pi + Alexa Voice Servce (Python)
Use python on Raspberry Pi 3 to light the LED with switch control!
I tried to make a traffic light-like with Raspberry Pi 4 (Python edition)
I connected the thermo sensor to the Raspberry Pi and measured the temperature (Python)
Stream processing for Python and SensorTag, Kafka, Spark Streaming --Part 1: Raspberry Pi 3
The temperature is automatically measured using Raspberry Pi 3 and automatically uploaded to the server (Docker python3 + bottle + MySQL) for viewing!
Connect two USB cameras to Raspberry Pi 4
Detect "brightness" using python on Raspberry Pi 3!
Raspberry Pi Security Infrared Camera (Python Edition)
Adafruit Python BluefruitLE works on Raspberry Pi.
Driver script for parametrically calculating Python scripts
Try fishing for smelt with Raspberry Pi
[Python] Organizing how to use for statements
Run servomotor on Raspberry Pi 3 using python
Python> Output numbers from 1 to 100, 501 to 600> For csv
How to use "deque" for Python data
How to install NumPy on Raspberry Pi
An introduction to Python for C programmers
Working with GPS on Raspberry Pi 3 Python
I tried to create a button for Slack with Raspberry Pi + Tact Switch
Try to use up the Raspberry Pi 2's 4-core CPU with Parallel Python
Use Raspberry Pi Python to TMP36 analog temperature sensor and MCP3008 AD converter