I have a temperature sensor connected to rasberry pi pico and have a main.py, which records the temperature data into .txt file. When I connect the rasberry pi to PC via USB port, it starts recording and saves the data in one .txt file. However, when I connect it to an external power supply (AC DC adapter with output 5V), the data is saved in multiple .txt files of different sizes (named logfile_0001.txt, logfile_0002.txt, ... as expected). Main.py starts running whenever the rasberry pi is connected to the external power supply, and I can confirm that by the LED, which I programmed it to blink when it starts collecting data and after writing the first three lines. The weird thing is, I never observe LED blinking more than the initial blink right when it is connected to the external power, even though it should whenever it writes the three header lines. Each separate txt file does contain those three lines as headers. I am very confused how the file can have the three lines without LED blinking, too.
Here is the code after setting up the i2c using machine module in micropython:
#naming the file
num = 0
for file in os.ilistdir():
file_name = file[0]
if not file[0].startswith('logfile'):
continue
temp = int(file_name[8:13])
if temp > num:
num = temp
num += 1
file_name = f'logfile_{num:05d}.txt'
with open(file_name,'w') as f:
f.write('Humidity and temperature data taken using T9602 Humidity & Temperature Sensor, on Rasberry Pi Pico\n')
f.write(str(time.localtime(start_time)) +'\n')
f.write('unixTime Humidity1(%) Temperature1(C) Humidity2(%) Temperature2(C)\n')
#LED blinks when connected
if True:
led_onboard.value(1)
for i in range(3):
led_onboard.value(0)
utime.sleep(0.3)
led_onboard.value(1)
utime.sleep(0.3)
led_onboard.value(0)
while True:
#turn on LED when recording data
led_onboard.value(1)
#communicate with two i2c devices (sensors)
i2c.writeto(address[0],b'1')
i2c2.writeto(address2[0],b'1')
#calculating temperatures based on the data read
RH1, TH1 = data_calc(i2c.readfrom(address[0],4))
with open(file_name, 'a') as f:
f.write(f'{time.time()} {RH1} {TH1} {RH2} {TH2}\n')
print(f'{time.localtime(time.time())[:-2]} {RH1} {TH1} {RH2} {TH2}')
time.sleep(1)
If someone have any insights, I would greatly appreciate it! Thank you so much.