WiringPi-SPI communication using Python

Connect the MCP3002 ADC to the Raspberry Pi2 with "Connect the A / D converter MCP3002 to the Raspberry Pi" so that the analog sensor value can be read via SPI. did. I used py-spidev to perform SPI communication from python, but python wrapper of WiringPi v2 You can also control the SPI using, so here I will use WiringPi2 python to read the analog sensor via SPI.

Experimental circuit

--Connect the MCP3002 to SPI0.0 on the Raspberry Pi2 and connect the pressure sensor FSR400 to channel 0 on the MCP3002. --Connect the LED to GPIO 25.

FSR_prototype2_ブレッドボード.png

FSR_prototype2_回路図.png

SPI / GPIO control using WiringPi

Implement a program in python that reads the value of the pressure sensor from the MCP3002 and blinks the LED when a value exceeding the THRESHOLD value is observed.

Read using SPI Library

If you send 2 bytes of 0x68,0x00 according to the MCP3002 data sheet, the sensor value of 2 bytes connected to CH0 (valid is the lower 10 bits) will be returned. If 0x78 is used instead of 0x68, the sensor value connected to CH1 can be obtained.

read_adc_wp2.py


#!/usr/bin/env python3

import wiringpi2 as wp
import time

# SPI channel (0 or 1)
SPI_CH = 0

# SPI speed (hz)
SPI_SPEED = 1000000

# GPIO number
LED_PIN = 25

# threshold
THRESHOLD = 200

# setup
wp.wiringPiSPISetup (SPI_CH, SPI_SPEED)
wp.wiringPiSetupGpio()
wp.pinMode(LED_PIN, wp.GPIO.OUTPUT)

while True:
    buffer = 0x6800
    buffer = buffer.to_bytes(2,byteorder='big')
    wp.wiringPiSPIDataRW(SPI_CH, buffer)
    value = (buffer[0]*256+buffer[1]) & 0x3ff
    print (value)

    if value > THRESHOLD:
      wp.digitalWrite(LED_PIN, wp.GPIO.HIGH)
      time.sleep(0.2)
      wp.digitalWrite(LED_PIN, wp.GPIO.LOW)
      time.sleep(0.2)

    time.sleep(1)

Use MCP3002 Extension

Since WiringPi2 has an extension of MCP3002, it can be used to read the value of the analog sensor without being aware of SPI communication (PIN_BASE is CH0, PIN_BASE + 1 is CH1).

read_mcp3002.py


#!/usr/bin/env python3
###
### read MCP3002 ADC analog value via RasPi SPI
###

import wiringpi2 as wp
import time

# SPI channle (0 or 1)
SPI_CH = 0

# pin base (above 64)
PIN_BASE=70

# GPIO number
LED_PIN = 25

# threshold
THRESHOLD = 200

# setup
wp.mcp3002Setup (PIN_BASE, SPI_CH)
wp.wiringPiSetupGpio()
wp.pinMode(LED_PIN, wp.GPIO.OUTPUT)

# if a sensor value is over THRESHOLD,
# flash led.
while True:
    value = wp.analogRead(PIN_BASE)
    print (value)

    if value > THRESHOLD:
      wp.digitalWrite(LED_PIN, wp.GPIO.HIGH)
      time.sleep(0.2)
      wp.digitalWrite(LED_PIN, wp.GPIO.LOW)
      time.sleep(0.2)

    time.sleep(1)

Run

Running

References

Recommended Posts

WiringPi-SPI communication using Python
Scraping using Python
Socket communication using socketserver with python now
[Python] [Windows] Serial communication in Python using DLL
Operate Redmine using Python Redmine
Serial communication with python
Fibonacci sequence using Python
Data analysis using Python 0
Data cleaning using Python
Using Python #external packages
Age calculation using python
HTTP communication with Python
Search Twitter using Python
Name identification using python
Notes using Python subprocesses
Try using Tweepy [Python2.7]
Pass the authentication proxy through communication using python urllib3
Python notes using perl-ternary operator
Flatten using Python yield from
Scraping using Python 3.5 async / await
TCP communication using Socket module-Python3
Save images using python3 requests
[S3] CRUD with S3 using Python [Python]
[Python] Try using Tkinter's canvas
Using Quaternion with Python ~ numpy-quaternion ~
Try using Kubernetes Client -Python-
Python notes using perl-special variables
[Python] Using OpenCV with Python (Basic)
Scraping using Python 3.5 Async syntax
Website change monitoring using python
Post to Twitter using Python
Start to Selenium using python
Search algorithm using word2vec [python]
Introduction to serial communication [Python]
Change python version using pyenv
python: Basics of using scikit-learn ①
# 1 [python3] Simple calculation using variables
Create JIRA tickets using Python
Manipulate spreadsheets locally using Python
Python memo using perl --join
View Python communication with Fiddler
Web scraping using Selenium (Python)
[Python] I tried using OpenPose
[Python] JSON validation using Voluptuous
Asynchronous http communication using asyncio
Broadcast on LINE using python
Data analysis using python pandas
Translate using googletrans in Python
Using Python mode in Processing
Using OpenCV with Python @Mac
[Python] Shooting game using pyxel
Send using Python with Gmail
Serial communication control with python and I2C communication (using USBGPIO8 device)
Serial communication control with python and SPI communication (using USBGPIO8 device)
How to install python using anaconda
Harmonic mean with Python Harmonic mean (using SciPy)
Initializing global variables using Python decorators
[Python] Loading csv files using pandas
GUI programming in Python using Appjar
Socket communication with Python LEGO Mindstorms
Retry post request using python requests