Pyboard writing to tvt/csv file

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
mironk
Posts: 11
Joined: Sun Jan 26, 2020 10:27 pm

Pyboard writing to tvt/csv file

Post by mironk » Sun Jan 26, 2020 10:39 pm

Hallo,
I am begginer with micropython. I am trying to write a adc data to file using PYBv1.1
My code is as follows:

Code: Select all

from pyb import Pin, ADC
import time
f = open('datafile.csv', 'w')
for i in range(0,1000):
    tm1 = pyb.millis()
    adc = ADC(Pin('X1'))
    if adc > 1400:
        f.write(str(tm1) + "," + str(adc.read()) +'\n')
f.close()
The first problem is that I need to restart pyboard at list twice to get data recorded to file.
The second problem is the statement 'if' that makes an error (blinking red and green leds). Without if condition all data are recorded.
Can anyone help me to solve the problem?
Thanks
Miron

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Pyboard writing to tvt/csv file

Post by jimmo » Mon Jan 27, 2020 3:48 am

The reason for the restart is that the filesystem doesn't synchronise with the host PC until it is re-mounted on the host. Peter explained this recently here: viewtopic.php?f=2&t=7542&p=42993#p42972

The reason the `if` statement is failing is that it needs to be `adc.read() > 1400` not just `adc > 1400` (you need to compare the value, not the actual adc object). I'd probably suggest changing it to:

Code: Select all

sample = adc.read()
if sample > 1400:
  f.write(str(tm1) + "," + str(sample) +'\n')
Also, no need to recreate the ADC object every time:

Code: Select all

from pyb import Pin, ADC
import time
f = open('datafile.csv', 'w')
adc = ADC(Pin('X1'))
for i in range(0,1000):
    tm1 = pyb.millis()
    sample = adc.read()
    if sample > 1400:
        f.write(str(tm1) + "," + str(sample) +'\n')
f.close()
If you're using firmware v1.12, I'd probably recommend using machine.ADC rather than pyb.ADC (we're planning to move all the pyb APIs into machine, to match the other ports). It's almost identical, other than you use the read_16() method instead (which returns a scaled 16-bit value).

Code: Select all

from machine import Pin, ADC
import time
f = open('datafile.csv', 'w')
adc = ADC(machine.Pin.board.X1)
for i in range(0,1000):
    tm1 = pyb.millis()
    sample = adc.read_16()
    if sample > 1400:   # You'll need to change this value to whatever the 16-bit equivalent is.
        f.write(str(tm1) + "," + str(sample) +'\n')
f.close()
Also I'd recommend adding a time.sleep_ms() to that loop so you can get values spaced at the right sample rate.

mironk
Posts: 11
Joined: Sun Jan 26, 2020 10:27 pm

Re: Pyboard writing to tvt/csv file

Post by mironk » Mon Jan 27, 2020 5:15 pm

Thanks for the help. I have some improvement using your code, below. Generally, it works fine but some things occur.
1. The maximum number of iterations is between 230 and 240. Otherwise, the file created is empty.
2. The "if" statement is let's say ignored. In my case, when I open the file, I can see ADC (sample reads) lower than 1400?
Is there any way to fix it? Bu the way I am using the firmware v1.9.4.
Best regards
Miron

Code: Select all

from pyb import Pin, ADC
import time
f = open('datafile.csv', 'w')
adc = ADC(Pin('X1'))
for i in range(0,1000):
    tm1 = pyb.millis()
    sample = adc.read()
    if sample > 1400:
        f.write(str(tm1) + "," + str(sample) +'\n')
f.close()

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Pyboard writing to tvt/csv file

Post by jimmo » Tue Jan 28, 2020 1:32 am

mironk wrote:
Mon Jan 27, 2020 5:15 pm
1. The maximum number of iterations is between 230 and 240. Otherwise, the file created is empty.
I'm not quite sure what you mean. My guess would be that either you're running out of space or something's going wrong with syncing the USB drive.

One thing that might be worth doing is to open-for-append/write/close the file for every iteration rather than keeping the file open across iterations.
mironk wrote:
Mon Jan 27, 2020 5:15 pm
2. The "if" statement is let's say ignored. In my case, when I open the file, I can see ADC (sample reads) lower than 1400?
Is there any way to fix it? Bu the way I am using the firmware v1.9.4.
You should definitely update to 1.12, there are lots of fixes since 1.9.4

I'm not sure what you mean by "the if statement is ignored". Can you share your exact code?

mironk
Posts: 11
Joined: Sun Jan 26, 2020 10:27 pm

Re: Pyboard writing to tvt/csv file

Post by mironk » Thu Jan 30, 2020 7:49 pm

Hi,
The append helped and now I can write at least 1000 lines. Thanks.
Unfortunatelly the "if" statement does not work. I want to discard values under 1400 but it does not work. They are also recorded to the file..

Code: Select all

from pyb import Pin, ADC
import time
f = open('datafile.csv', 'a')
adc = ADC(Pin('X1'))
for i in range(0,1000):
    tm1 = pyb.millis()
    sample = adc.read()
    if sample > 1400:
        f.write(str(tm1) + "," + str(sample) +'\n')
f.close()
Best regards
Miron

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Pyboard writing to tvt/csv file

Post by dhylands » Thu Jan 30, 2020 8:53 pm

I tried your code and it wrote 833 lines, none with a value less than 1400. I had to add an import pyb to the top since it doesn't work without it. I was running on a PYBV10.

mironk
Posts: 11
Joined: Sun Jan 26, 2020 10:27 pm

Re: Pyboard writing to tvt/csv file

Post by mironk » Fri Jan 31, 2020 9:26 am

Thanks for your tips. I started to test on different computers. The problem was not in "if" statement but in my regional settings. I had decimal separator "," despite "." . So then Excel treated "time,ADC" as one number and reduced the zero on the end :)
Now everything works perfect.

Post Reply