Detect analog signals with A / D converter using python on Raspberry Pi 3!

Introduction

My name is Ryosuke Kamei and I am an IT consultant based on the idea of "gentle IT"! Currently, in my work, I am doing upstream processes focusing on requirements analysis and requirements definition, but I also write programs! As part of our friendly IT activities, we will introduce "Raspberry Pi 3" (commonly known as Raspberry Pi), which has the philosophy of "providing inexpensive PCs that can be programmed for education"!

This time, we will detect the analog signal converted by the A / D converter! [Razpai Magazine-June 2016 Special Feature 1 Akiba's Popular Parts Wiring Diagram Best 17 11 Can Read Analog Signals](https://www.amazon.co.jp/ Razpai Magazine-June 2016 Issue-Nikkei BP PC Best Mook -? Nikkei Linux-ebook / dp / B01EH2RX42 / ref = as_li_ss_tl ie = UTF8 & dpID = 61P3o6Agz + L & dpSrc = sims & preST = OU09__BG0,0,0,0_FMpng_AC_UL160_SR120,160 & psc = 1 & refRID = 31SKG5E0DGKBSF9BWNGV & linkCode = ll1 & tag = sr2smail-22 & linkId = 18fd04738627edef8d6fffd6c9b8f9c2) Is referred to.

procedure

  1. Wiring
  2. Program adc.py
  3. Run the program

1. Wiring

The wiring diagram is [Razpai Magazine-June 2016 Issue Special Feature 1 Akiba's Popular Parts Wiring Diagram Best 17 11 Can Read Analog Signals](https://www.amazon.co.jp/ Razpai Magazine-2016 6 Monthly issue-Nikkei BP PC Best Mook-Nikkei Linux-ebook / dp / B01EH2RX42 / ref = as_li_ss_tl? 22 & linkId = 18fd04738627edef8d6fffd6c9b8f9c2) As it is.

If you get an image like this, referring to the picture of the circuit Raspberry Pi 3でpythonを使い::Dコンバーターでアナログ信号を検出する!回路.jpg

I also made a video Click the video commentary →

Text version

From the Raspberry Pi side

From the Raspberry Pi side

--Raspberry Pi Pin 1 (3.3V) --AD Converter 8 Vdd --Semi-fixed resistor left side --Raspberry Pi 19th pin (MOSI) --AD converter 5 MOSI --Raspberry Pi 21st pin (MISO) --AD converter 6 MISO --Raspberry Pi 23rd pin (SCLK) --AD converter 7 CLK --Raspberry Pi 24th pin (CE0) --AD converter 1 CS --Raspberry Pi 6th pin (GND) --AD converter 4 Vss --Semi-fixed resistor right side

From AD converter (difference only)

--AD converter CH0 --Semi-fixed resistor center

2. Program adc.py

Program is also [Razpai Magazine-June 2016 Issue Special Feature 1 Akiba's Popular Parts Wiring Diagram Best 17 11 Can Read Analog Signals](https://www.amazon.co.jp/ Razpai Magazine-June 2016 Issue -Nikkei BP PC Best Mook-Nikkei Linux-ebook / dp / B01EH2RX42 / ref = as_li_ss_tl? Ie = UTF8 & dpID = 61P3o6Agz + L & dpSrc = sims & preST = _OU09__BG0,0,0,0_FMpng_AC_UL160_SR120,0_FMpng_AC_UL160_SR120 It is based on 18fd04738627edef8d6fffd6c9b8f9c2).

The source is uploaded to GitHub, so please use it as you like.

Clone with git


$ git clone https://github.com/RyosukeKamei/raspberrypi3.git

adc.py


#Library to control GPIO
import wiringpi
#Timer library
import time

#Specify the channel to which MCP3002 (semi-fixed resistor) is connected
SPI_CH = 0

#Specify the analog input channel of the MCP3002 (semi-fixed resistor) to be read
READ_CH = 0

#SPI initialization
wiringpi.wiringPiSPISetup( SPI_CH, 1000000 )

while True:
    #Create data to send to MCP3002 (semi-fixed resistor)
    buffer = 0x6800 | ( 0x1800 * READ_CH )
    buffer = buffer.to_bytes( 2, byteorder='big' )
    
    #Get the value of CH0 using SPI
    wiringpi.wiringPiSPIDataRW( SPI_CH, buffer )
    #The values are sent in 2 bytes, so combine them into one value.
    ch0_value = ( buffer[0] * 256 + buffer[1] ) & 0x3ff
    #Convert value to voltage
    volt = ch0_value * 3.3 / 1023
    # 0.Every 5 seconds
    time.sleep(0.5)
    print ("value:" , ch0_value , "Voltage:", volt , "V")

3. Run the program

I recorded the video of how it is moving. Click the video commentary →

Sound the buzzer for 5 seconds


$ sudo python3 adc.py

site map

Raspberry Pi 3 setup

Install Raspberry Pi 3 → Wireless LAN → Japanese input / output → Operate from Mac

Build a Python + MySQL environment with Docker on Raspberry Pi 3!

Install Docker on RaspberryPi3 Build a Python + bottle + MySQL environment with Docker on RaspberryPi3![Easy construction] Build a Python + bottle + MySQL environment with Docker on RaspberryPi3![Trial and error]

Make an air conditioner integrated PC "airpi" with Raspberry Pi 3!

Make an air conditioner integrated PC "airpi" with Raspberry Pi 3!

Let's play with Raspberry Pi 3 and python

Programming with Node-RED programming with Raspberry Pi 3 and programming normally Light the LED with python on Raspberry Pi 3 (Hello World) Detect switch status on Raspberry Pi 3 Run a servo motor using python on Raspberry Pi 3 Control the motor with a motor driver using python on Raspberry Pi 3! Detect slide switch using python on Raspberry Pi 3! Detect magnet switch using python on Raspberry Pi 3! Detect temperature using python on Raspberry Pi 3! Sound the buzzer using python on Raspberry Pi 3! Detect analog signals with A / D converter using python on Raspberry Pi 3! Detect "brightness" using python on Raspberry Pi 3! Detect "temperature (using A / D converter)" using python on Raspberry Pi 3! Output to "7-segment LED" using python on Raspberry Pi 3!

Rules focused on test-driven development

Coding rules "Let's write gentle code" (FuelPHP) Naming convention "Friendly to yourself, team-friendly, and unseen members after 3 months"

Web application development with Docker + Python

Install Python3, related libraries pip, virtualenv and frameworks Django, bottle, Flask on CentOS on Docker! With a Dockerfile that summarizes these!

Easy to develop environment construction (Docker + PHP)

PHP environment + Eclipse is linked to Apache using Docker Building FuelPHP development environment using Docker Create CRUD skeleton using initial settings of FuelPHP development environment using Docker and scaffold FuelPHP database migration

Recommended Posts

Detect analog signals with A / D converter using python on Raspberry Pi 3!
Detect "temperature (using A / D converter)" using python on Raspberry Pi 3!
Detect "brightness" using python on Raspberry Pi 3!
Detect temperature using python on Raspberry Pi 3!
Control the motor with a motor driver using python on Raspberry Pi 3!
Detect slide switches using python on Raspberry Pi 3!
Detect magnet switches using python on Raspberry Pi 3!
Using a webcam with Raspberry Pi
Run servomotor on Raspberry Pi 3 using python
Working with GPS on Raspberry Pi 3 Python
[Note] Using 16x2-digit character LCD (1602A) from Python with Raspberry Pi
Try using a QR code on a Raspberry Pi
Sound the buzzer using python on Raspberry Pi 3!
Connect to MySQL with Python on Raspberry Pi
Build a Python development environment on Raspberry Pi
[Raspberry Pi] Publish a web application on https using Apache + WSGI + Python Flask
Using the 1-Wire Digital Temperature Sensor DS18B20 from Python on a Raspberry Pi
Try debugging Python on Raspberry Pi with Visual Studio.
Ubuntu 20.04 on raspberry pi 4 with OpenCV and use with python
Output to "7-segment LED" using python on Raspberry Pi 3!
Access google spreadsheet using python on raspberry pi (for myself)
getrpimodel: Recognize Raspberry Pi model (A, B, B +, B2, B3, etc) with python
Use vl53l0x with Raspberry Pi (python)
Solve ABC166 A ~ D with Python
Try using ArUco on Raspberry Pi
Notes on using rstrip with python.
Detect switch status on Raspberry Pi 3
How to upload a file to Cloud Storage using Python [Make a fixed point camera with Raspberry PI # 1]
Build a Tensorflow environment with Raspberry Pi [2020]
Adafruit Python BluefruitLE works on Raspberry Pi.
A memo with Python2.7 and Python3 on CentOS
Map rent information on a map with python
Programming normally with Node-RED programming on Raspberry Pi 3
Solve AtCoder ABC168 with python (A ~ D)
Working with sensors on Mathematica on Raspberry Pi
Make a wash-drying timer with a Raspberry Pi
Operate an oscilloscope with a Raspberry Pi
Create a car meter with raspberry pi
CSV output of pulse data with Raspberry Pi (confirm analog input with python)
Use python on Raspberry Pi 3 to light the LED with switch control!
Try tweeting arXiv's RSS feed on twitter from Raspberry Pi with python
Make a wireless LAN Ethernet converter and simple router with Raspberry Pi
[Python + PHP] Make a temperature / humidity / barometric pressure monitor with Raspberry Pi
I tried to make a traffic light-like with Raspberry Pi 4 (Python edition)
Create a Python multi-user platform with JupyterHub + JupyterLab on Rapsberry Pi 3B +!
Finally ... Make a radio control using python on Raspberry Pi 3! (The motor moves while the button is pressed)
Make a thermometer with Raspberry Pi and make it visible on the browser Part 3
Discord bot with python raspberry pi zero with [Notes]
Finally ... Make a radio control using python on Raspberry Pi 3! (The motor moves while the magnet is brought closer)
Build a Django environment on Raspberry Pi (MySQL)
Build a python environment with ansible on centos6
I tried L-Chika with Raspberry Pi 4 (Python edition)
Solve A ~ D of yuki coder 247 with python
Enjoy electronic work with GPIO on Raspberry Pi
Folium: Visualize data on a map with Python
Power on / off your PC with raspberry pi
Creating a temperature control system with Raspberry Pi and ESP32 (3) Recipient Python file
Get CPU information of Raspberry Pi with Python
Make DHT11 available on Raspberry Pi + python (memo)
Create a visitor notification system using Raspberry Pi
Play with your Ubuntu desktop on your Raspberry Pi 4