Home>

I succeeded in getting the data from channel 0 with the program shown below. I would like to measure the data up to channel 4 as well as this work. However, I don't know how to write a program that measures by increasing the number of channels. Can you tell me something if you like? The sensor is myoware. The AD converter used is mcp3204. Please give me some advice!

import wiringpi as pi
import time
import numpy as np
import openpyxl as excel
import spidev
t1 = time.time ()
SPI_CE = 0
SPI_SPEED = 5000000
READ_CH = 0
VREF = 5.0
Vi = []
i = 0
spi = spidev.SpiDev ()
spi.open (0,0)
################################
adc = spi.xfer2 ([6,0,0])
################################
def do_sampling (i): # Voltage acquisition
    y [i] = time.perf_counter () --time_start

sampling_num = 1000
sampling_rate = 2000
sampling_interval = 1/sampling_rate
print (sampling_interval)
x = np.arange (0, sampling_num/sampling_rate, sampling_interval)
y = np.zeros (sampling_num)
count = 0
time_start = time.perf_counter ()
time_next = time_start
while count<sampling_num:
    do_sampling (count)
    time_next + = sampling_interval
    value = ((adc [1]&15)<<8) + adc [2]
    volt = value/4096
    Vi.append (volt)
    while (time.perf_counter ()<time_next): # Wait until the next time.
        pass
    count = count + 1
print (time.time ()-t1)
wb = excel.Workbook ()
ws = wb.active
for i in range (1, sampling_num):
    s = y [i]
    T = Vi [i-1]
    ws.cell (row = i, column = 1, value = s)
    ws.cell (row = i, column = 2, value = T)
wb.save ("denatsu.xlsx")
  • Answer # 1

    I haven't confirmed it because I don't have the actual item, but if you look here, you can see various things. (Actually, it should be a reference for the library, but for the time being ...)

    In short
    adc = spi.xfer2 ([6,0,0])This is the reading of ch0. Maybe you can read it with xfer
    adc = spi.xfer2 ([6,0x40,0])
    adc = spi.xfer2 ([6,0x80,0])
    adc = spi.xfer2 ([6,0xc0,0])
    Then, it seems that it means reading ch1,2,3 respectively.