0:13
everyone welcome to another video in the
0:16
raspberry pi pico series
0:19
in this video we will cover how to use i
0:22
c with the pico of course
0:25
i will first demonstrate the simple i
0:28
device and later we will interface a 16
0:34
the device i am using for this video is
0:39
it's an i squared c device obviously
0:43
you can see the input pins here clock
0:50
it have 16 pins which connects to the
0:54
but that part will come later out of
0:58
pins these seven pins are the actual
1:00
pins where we can control the output
1:04
we have a group of p4 to p7 and another
1:10
p3 is the pin after p7 and i am not
1:13
using it in this section of the tutorial
1:17
let's say if we send a 1 to this device
1:19
the pin p0 will be set high
1:23
similarly if we send a 2 the pin p1 will
1:28
sending 4 will set the p2 high
1:33
and similarly sending a 16 32
1:37
64 and 128 will turn on the higher four
1:41
pins i will demonstrate this in a minute
1:45
let's start the thunni id
1:52
i will connect my pico and select this
1:56
you can see the pico is connected now
2:03
let's start by importing the i squared c
2:16
import time for delay purposes
2:19
before we go any further let's see the
2:24
i am going to use the i2c one for this
2:28
as you can see the pins 27 is the clock
2:32
and 26 is the data pin
2:35
also i am going to power the device with
2:37
this 3.3 volts and ground
2:41
now first we will create an i2c object
2:45
the first parameter is the i2c bus and
2:50
then the clock pin data pin
2:54
and at last the frequency let's keep it
3:00
now we will scan the device for its
3:04
we need to call the i2c scan for this
3:11
save the file directly to the pico
3:22
seems like i typed in the wrong keyword
3:26
okay so we got 39 here this is the
3:29
address of our slave device
3:38
this is the data sheet for the pcf-8574
3:44
here you can see bunch of addresses are
3:47
actually the address of the device can
3:49
be modified up to some extent
3:53
this depends on these three pins a0
4:00
next pins are the ground pins
4:03
if you connect any of these pins to the
4:05
ground the address of the device will
4:08
and that explains all these high and low
4:13
right now all three pins are high so the
4:16
address will be 0 cross 4
4:18
e or 78 but that's not the address we
4:22
were shown in the console
4:25
actually this here is the 8-bit address
4:29
which consists of the 7-bit address plus
4:34
to indicate the type of operation
4:37
pico just like arduino uses the 7 bits
4:41
excluding the read or write bit so to
4:46
we need to shift this address by one
4:50
so we have 1 1 1 0 for the lower 4 bits
4:54
and 0 1 0 for the upper 3 bits
4:57
this makes up 0 cross 2 7 which is 39 in
5:05
let's write our program now
5:13
here i am shifting the one by four
5:15
places and storing it in the value
5:19
as i explained in the beginning that we
5:21
can control these seven output pins from
5:25
i have four leds connected to these four
5:29
so in order to set the pin p4 i need to
5:32
write a one in that position
5:34
and to do that i am shifting the one by
5:39
now use right to function to write the
5:43
first parameter is the device address
5:46
and then the buffer to the data
5:48
you want to send i am using bytes to
5:51
convert the value to the byte
5:58
you can see the first led is on
6:03
and if we send one to the fifth position
6:17
and the last one is on now
6:24
if we want to turn all of them on well
6:28
which corresponds to 1 1 1 1.
6:34
you see all the leds are on now
6:37
let's create a function to do this task
6:43
i am using the for loop in the range
6:47
here 8 is not included in the range
6:50
now shift the 1 according to the value
6:55
and send this value to the device
6:58
we will repeat this every 500
7:02
now define the main function where the
7:04
above function will be in the while loop
7:08
and this line to call the main function
7:13
let's run it we have an error in pcf
7:17
led it's because of this spelling
7:22
okay let's try it again
7:27
you can see the leds are blinking every
7:33
so everything is good this is how you
7:36
can use the i squared c
7:43
that's it for the first part
7:48
now in the second part of this video we
7:50
will interface the lcd 16x2 using the
7:58
i have created a python file for this
8:00
and i will explain all the steps
8:07
first of all we will import the i2c and
8:15
i am using the datasheet which is
8:19
in order to interface this lcd or any
8:23
we have to send some commands and some
8:27
these are the functions defined to do so
8:34
here you can see how the pcf 8574
8:37
is connected to the lcd this rs pin
8:42
whether the value sent is a command or
8:46
basically to send the command this pin
8:50
and to send the data it must be high
8:54
is is connected to the pin p0 of the pcf
8:58
then another important thing you need to
9:00
know is the pins d4 to d7 of the lcd
9:04
are connected to the pins p4 to p7 of
9:08
we are going to use the display in the
9:11
so we only need 4 data pins
9:15
the lower data pins are not connected to
9:19
since we are only using 4 data pins we
9:22
will separate the 8-bit command in the
9:26
the upper half and the lower half
9:29
here i am adding the upper half with the
9:32
zero c the reason is simple
9:38
whenever we send the command or data to
9:42
we must turn the enable bit high and
9:46
this enable strobe will make the data
9:48
transferred to the lcd
9:51
now here i am keeping the enable pin
9:53
high that means the p2 is high
9:56
this will make the zero cross zero four
10:00
also like i said in the beginning the
10:02
backlight of the display is connected to
10:06
so we need to keep it high 2 and since
10:09
the rs pin must be low that makes the p0
10:14
adding them together we have to keep the
10:17
3 high and p 0 as low
10:21
this makes up 0 cross 0 c
10:24
after sending it we will send 0 cross 0
10:28
basically the same as last case but this
10:31
time the enable is low
10:34
remember the enable strobe first keep it
10:37
then low this will transfer the upper
10:42
in the same way we will transfer the
10:46
to first send with enable high
10:49
and then with enable low and
10:52
finally we will write the values to the
10:54
i2c device in this sequence
11:03
this print function here is just kept
11:05
for debugging purpose
11:08
in case you want to check if the
11:09
commands are correct or not
11:12
sending data is similar to command
11:15
we separate the upper half and the lower
11:19
then we add with zero cross zero d this
11:26
this is because while sending the data
11:29
we must keep the rs pin high
11:32
this will tell the display that we are
11:34
sending data this time
11:37
other than that we have to do the same
11:39
like sending command
11:46
send with enable hi and then send with
11:54
then here we have the lcd initialization
11:59
this sequences as per the instructions
12:01
given in the datasheet of the display
12:15
this is for 8-bit interface but since we
12:19
four pins we have to use this one
12:23
as mentioned here we have to wait for
12:25
around 15 milliseconds
12:27
or 40 milliseconds then send this
12:32
as you can see here the command is
12:34
written for db4 to db7
12:38
these are the higher data pins in our
12:41
so basically this command will transform
12:55
then wait for more than four
12:56
milliseconds and send the same command
13:00
again wait for 100 microseconds and send
13:06
as you can see i am following the
13:10
and then there are other instructions as
13:14
you can configure it according to your
13:18
like if you want one line or two lines
13:20
or cursor should be a line or block
13:23
or should it be blinking or shifting the
13:27
etc just configure according to the
13:31
requirement you have
13:32
the commands are explained pretty well
13:37
this is the function to send the string
13:40
here we use the for loop to keep sending
13:42
individual data to the display
13:45
ord function is used to change any
13:48
character to the respective decimal
13:53
then we have the go to function this is
13:57
to set the cursor at different places on
14:01
here x is the row and y is column
14:05
here we simply add the column number to
14:07
the zero cross eight zero
14:08
which is the zeroth row or zero cross c
14:12
which is the first row and at last this
14:15
is the command to clear the display
14:19
now let's see the main file
14:33
we first have to import the library
14:38
let's save the library in the pico first
14:50
okay now we will initialize the display
14:54
go to zeroth row and first column
14:57
print something go to first row first
15:02
and print another thing let's save it to
15:23
you can see the data is displaying at
15:26
the respective locations
15:30
now let's assume you want to send the
15:32
number or a float value to this display
15:36
i am going to send 123.
15:44
clear the display and go to 0 1
15:48
and send the number by converting it to
15:54
do the same for float
16:10
as you can see the data is displaying as
16:14
this is it for this video i know the
16:18
is not written according to the python
16:20
standard but i am more
16:22
of a c person i don't have a very good
16:25
command over python yet
16:27
but this works well for now maybe in the
16:32
as i get more experienced with python i
16:34
will update these libraries
16:37
you can download the code from the
16:39
github the link is in the description
16:42
keep watching and have a nice day ahead