Hi, welcome to the last lecture in this section, in this lecture.
I'll show you how to read a value from a potential error and then use it to drive.
And using GWM this potential race, regular 10 kiloton potential murder.
I have connected to your 34 right here.
And I've got one pin going to the three point three volt power rail and the other one to ground.
Let's have a look at the sketch.
There's a couple of interesting things happening here.
First of all, as you can see here in my notes, we will be using the ADC, the analog to digital converter,
to take readings from the potential mirror, and then we'll convert those readings to an appropriate
peak in value.
Now, the thing to remember is that by default, ATC producer values 12 bits, which means that the
range of an ATC value goes from zero to four thousand and ninety five.
And as you've seen in the previous election, the Section PITIABLY and Values Pettifor have 10 bits
in width, so values ranging from zero to one thousand point twenty three.
And therefore we need to do a little calculation to scale the ADC value into a P value.
And this calculation is simply divide the range of the M by the range of the ADC and that will give
us the scaling factor, which happens to be three point twenty four, probably closer to zero point
twenty five.
But OK.
And we'll see what values come out later.
And that means that you just multiply whatever comes out of the ADC by zero point twenty four and that
will give you a new value that is within the range.
I also have the documentation for the ATC here.
I'm going to refer to this in a moment.
And there's also documentation for a built in python function called E.A. Integer that converts floating
point in this into integers.
Again, we need to make this conversion from floating point to integer in sketchiest.
You'll see in a moment.
Let's have a look at the script.
So we need to first import ADC pain and P m from the machine module and sleep from the time module.
Here I'm using sleep, which allows me to define a sleep time in seconds instead of milliseconds of
time.
In some of the previous scripts in line thirty nine, I create the M object ytterbium twenty one.
Just to make it easy for me to remember that this is connected to Shapir twenty one and for the ADC
I'm using pin thirty four as I said earlier and using ADC constructor in the object.
Now here's one interesting way to configure the attenuation of your ADC.
The analog to digital converter is to use the atin function.
You can find out what the available attenuation levels by having a look at the documentation but hyperlink
to right here.
Let's check it out.
It is this section of the document and there is the attend function and these are the available attenuation.
So zero degrees with those two and a half decibel, six and eleven.
And I've gone for the eleven decibel attenuation which gives me maximum input voltage up to three point
six volts.
So depending on what it is that you are connecting, you can choose the appropriate attenuation.
You can also control the width of your analog to digital converter.
I'm not doing that here.
I'm just leaving it to its default.
Twelve page.
You can see you can go for nine, ten, eleven, twelve bits for the width.
OK, and moving on now, I've got a infinite loop here, while true, start by taking reading of the
potential murder, using the read function, then I multiplied by the scaling factor, your point twenty
four that I calculated up here.
And that gives me a floating point that I need to convert it to an integer using the anti function.
And I stole the result in PWI and then in line 47 or print out those two numbers so I can see the original
and the scaled number, then use that to set the short cycle for the entity.
And then take a little nap for zero point one seconds, OK?
Let's try this out, I'm going to get a copy of the script on the device log.
Great report, which is connected to Perio 34 Togepi y.
Yes, but an earlier version of this earlier this script to double click on it, you open it up, can
you get rid of the other two?
So that does confuse me as to which script I'm uploading to my hospitality.
And this is the one that I want to upload.
So let's do it.
You go right, you can see the current values for the potential.
Let's move at One Direction.
Then you saw moving the early days, becoming fainter to goes off, let's go to the other extreme.
Trying to do this in a nangle, simple, all right, going up in this chaos, and that's the maximum,
you can see that the potential murder is at four thousand ninety five is nine hundred and eighty two,
which means maybe I can increase this factor by maybe as much and that they allow me to go for the full
MGD cycle extent without going over it.
So save and play soldier one thousand twenty three point twenty five Scaling factor, which works out
perfectly.
Right.
So that's how you can use the ATC and your is pathetic to using micro python.