Reputation: 13
Code:
from machine import Pin
from machine import ADC
from time import sleep_ms
x = ADC(Pin(4, Pin.IN))
y = ADC(Pin(5, Pin.IN))
x.atten(ADC.ATTN_11DB)
y.atten(ADC.ATTN_11DB)
while True:
x_val = x.read()
y_val = y.read()
print('Current position:{},{}'.format(x_val,y_val))
sleep_ms(300)
Error:
Traceback (most recent call last):
File "<stdin>", line 5, in <module>
TypeError: can't convert Pin to int
I want to know what's my mistake.
I tried to concatenate str
to int
and int
to str
. It did not work.
Upvotes: 1
Views: 777
Reputation: 5121
The TypeError
means you are using an object (a variable) of a different type then expected. The error tells you that, in this case, on line 5, an int
is expected, but a Pin
was used in your code.
Line 5 of your code is:
x = ADC(Pin(4, Pin.IN))
and it contains a Pin
indeed.
Looking into the documentation of the machine.ADC
class shows this should be fine:
class machine.ADC(id, *, sample_ns, atten)
Access the ADC associated with a source identified by id. This id may be an integer (usually specifying a channel number), a Pin object, or other value supported by the underlying machine.
However, has the library always supported this?
Browsing through older versions of the library shows this has not always been the case. Up to version 1.11 it expected an int
:
class machine.ADC(id=0, *, bits=12)
Create an ADC object associated with the given pin.
So I assume you are using an old version of the library. To fix the problem you should either update your library to the latest version (at least v1.12), or you should read the documentation of the version you are using and update your code accordingly. For older versions, you should write
x = ADC(4)
y = ADC(5)
Upvotes: 2