-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdcmotor.py
50 lines (38 loc) · 1.2 KB
/
dcmotor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import time
import busio
import adafruit_ads1x15.ads1115 as ADS
from adafruit_ads1x15.analog_in import AnalogIn
import csv
'''
DC MOTOR
LEFT ----> RIGHT
WHITE BLACK
GPIO GROUND
'''
# Define the SCL and SDA pins
SCL_PIN = 3
SDA_PIN = 2
# Initialize the I2C interface
i2c = busio.I2C(SCL_PIN, SDA_PIN)
# Create an ADS1115 object with 15-bit mode and data rate
ads = ADS.ADS1115(i2c, address=0x48)
# Configure the ADC settings for 5V reference voltage
ads.gain = 1
ads.mode = ADS.Mode.CONTINUOUS
# Define the analog input channel
channel = AnalogIn(ads, ADS.P3)
# Define time variables for csv file
time_pass = 0
time_increment = 0.004
# Create csv to write data into
with open("dcmotor_data_test.csv", 'w', newline='') as csvfile:
csvwriter = csv.writer(csvfile)
csvwriter.writerow(["Time(s)", "Volts"])
# Loop to read the analog input continuously
while True:
analog_value = channel.value
voltage_output = (analog_value * 4.096) / (2**15)
print(f"Voltage Output: {voltage_output:.8f}")
csvwriter.writerow([f"{time_pass:.2f}", voltage_output])
time.sleep(time_increment)
time_pass += time_increment