Hello,
I recently started working with Raspberry Pi, python and ADC. I am currently using adc ADS1115 to convert a 1hz sine wave to digital values. I configure the register of the adc using the functions of adafruit_ads1x15 library and read the data from the conversion register using a function from smbus2 library. I have also attached the lines of code here for reference.
Now I read some values from the register and print them and these values don't look right to me.
Input to adc: 1Hz sine wave to AN0
Code:
import numpy as np
import adafruit_ads1x15.ads1115 as ADS
from adafruit_extended_bus import ExtendedI2C as I2C
import time
from smbus2 import SMBus
sampling_rate = 16
sampling_interval = 1 / sampling_rate
buffer = []
with SMBus(1) as bus:
i2c = I2C(1)
ads = ADS.ADS1115(i2c, gain=1)
ads._write_register(1, value=0b0100001000100100)
actual_start_time = time.perf_counter()
while len(buffer) < sampling_rate:
start_time = time.perf_counter()
#b = ads.get_last_result(fast=True)
b = bus.read_word_data(0x48, 0)
buffer.append(b)
stop_time = time.perf_counter()
difference = stop_time - start_time
if difference < sampling_interval:
time.sleep(sampling_interval - difference)
actual_stop_time = time.perf_counter()
actual_difference = actual_stop_time - actual_start_time
print("Actual Difference:", actual_difference)
buffer = np.array(buffer)
for i, sample in enumerate (buffer):
print(f"Sample {i}:{sample}")
Obtained output:
Actual Difference: 1.0010633140009304
Sample 0:18449
Sample 1:18449
Sample 2:47647
Sample 3:3886
Sample 4:54584
Sample 5:30269
Sample 6:30269
Sample 7:54074
Sample 8:36657
Sample 9:58659
Sample 10:7445
Sample 11:7445
Sample 12:50696
Sample 13:54785
Sample 14:64001
Sample 15:9993
Expected output: 16 discrete or different values
Any explanation or correction is much appreciated. Thank you in advance.
I recently started working with Raspberry Pi, python and ADC. I am currently using adc ADS1115 to convert a 1hz sine wave to digital values. I configure the register of the adc using the functions of adafruit_ads1x15 library and read the data from the conversion register using a function from smbus2 library. I have also attached the lines of code here for reference.
Now I read some values from the register and print them and these values don't look right to me.
Input to adc: 1Hz sine wave to AN0
Code:
import numpy as np
import adafruit_ads1x15.ads1115 as ADS
from adafruit_extended_bus import ExtendedI2C as I2C
import time
from smbus2 import SMBus
sampling_rate = 16
sampling_interval = 1 / sampling_rate
buffer = []
with SMBus(1) as bus:
i2c = I2C(1)
ads = ADS.ADS1115(i2c, gain=1)
ads._write_register(1, value=0b0100001000100100)
actual_start_time = time.perf_counter()
while len(buffer) < sampling_rate:
start_time = time.perf_counter()
#b = ads.get_last_result(fast=True)
b = bus.read_word_data(0x48, 0)
buffer.append(b)
stop_time = time.perf_counter()
difference = stop_time - start_time
if difference < sampling_interval:
time.sleep(sampling_interval - difference)
actual_stop_time = time.perf_counter()
actual_difference = actual_stop_time - actual_start_time
print("Actual Difference:", actual_difference)
buffer = np.array(buffer)
for i, sample in enumerate (buffer):
print(f"Sample {i}:{sample}")
Obtained output:
Actual Difference: 1.0010633140009304
Sample 0:18449
Sample 1:18449
Sample 2:47647
Sample 3:3886
Sample 4:54584
Sample 5:30269
Sample 6:30269
Sample 7:54074
Sample 8:36657
Sample 9:58659
Sample 10:7445
Sample 11:7445
Sample 12:50696
Sample 13:54785
Sample 14:64001
Sample 15:9993
Expected output: 16 discrete or different values
Any explanation or correction is much appreciated. Thank you in advance.
Statistics: Posted by Ananya — Thu Jul 25, 2024 1:44 pm — Replies 3 — Views 84