Roronoa Hatake - ESP32 and Arduino Hardware Protocols Cookbook - 2023
Roronoa Hatake - ESP32 and Arduino Hardware Protocols Cookbook - 2023
HARDWARE PROTOCOLS
COOKBOOK:
Practical Recipes to UART, SPI, I2C, and Non-
standard Protocols for Developing Connected
Devices
By
Roronoa Hatake
TABLE OF CONTENTS
PRACTICE BEFORE THEORY 5
DIFFERENTIATION FROM WIFI, BLUETOOTH ESP NOW 6
OPEN VSC PLATFORMIO PROGRAM IN ARDUINO IDE 7
DIFFERENCE OSCILLOSCOPE VS. LOGIC ANALYZER 10
LOGIC ANALYZER CHARACTERISTICS 12
PRACTICAL USE CASES AT A GLANCE 15
MASK WITH AND 17
MASK WITH OR 20
BITWISE SHIFT 22
PRACTICAL EXERCISE EXAMPLES 1 25
PRACTICAL EXERCISE EXAMPLES 2 32
WHY PULSEVIEW 34
DOWNLOAD 36
BASE SKETCH FOR ALL EXAMPLES 37
CONFIGURATION AND CONNECTING LOGIC
ANALYZER 38
SAMPLES AND MHZ SETTING 42
FREQUENCY AND MILLISECONDS 43
BAUD RATE 45
PRODUCE AND MEASURE PWM SIGNAL 47
TRIGGER ON LEVEL WITH IR SENSOR 51
OVERVIEW OF SERIAL COMMUNICATION WITH UART 54
THEORY OF DATA TRANSMISSION 58
HARDWARE SERIAL ARDUINO UNO 60
SOFTWARE SERIAL ARDUINO UNO 61
DISPLAY ASCII WITH DECODER 63
HEX TO ASCII WITH ARDUINO UNO 65
LSB AND MSB 68
BINARY LSB AND MSB WITH ARDUINO UNO 69
WHAT IS A PARITY BIT 74
EXAMPLE PARITY BIT SERIAL_8E1 WITH ESP32 76
UART COMMUNICATION BETWEEN TWO ARDUINO
UNOS 80
NOTE THE LOGIC LEVEL WITH ESP32 TO ARDUINO
UNO 84
FINDING OR CALCULATING THE BAUD RATE 88
ANALYZING THE IKEA AIR QUALITY SENSOR
VINDRIKTNING 91
SELF-MADE ANALYSIS WITH ESP32 FOR PM2,5 SENSOR FROM
IKEA 96
ADVANTAGES AND DISADVANTAGES OF UART 101
ONEWIRE PROTOCOL BASICS 103
DS18B20 SKETCH AND WIRING 104
EXAMPLE DUMMY CODE 108
INFO FROM THE DATA SHEET 110
DATA ANALYSIS WITH LOGIC ANALYZER 114
CHANGE RESOLUTION TO 9 BIT 116
ADDING AN EXTERNAL DECODER 117
GET DATA ONLY WITH ONEWIRE LIBRARY 120
CIRCUIT AND WIRING 123
UNDERSTANDING THE PROTOCOL AND SIGNAL
ANALYSIS 127
USING THE DHT11 DECODER 132
INSIGHT INTO THE DHT LIBRARY 133
BASICS 136
SKETCH ARDUINO UNO TO UNO 138
ANALYSIS WITH LOGIC ANALYZER 143
DHT22 VALUES VIA SPI 145
I2C INTRODUCTION 148
PROTOCOL IN DETAIL 150
SKETCH AND WIRING BH1750 152
ANALYSIS BH1750 155
SKETCH BH1750 WITHOUT EXTERNAL LIBRARY 159
SKETCH AND WIRING DHT20 165
ANALYSIS DHT20 167
SKETCH DHT20 WITHOUT EXTERNAL LIBRARY 170
ARDUINO UNO TO ARDUINO UNO 178
OVERVIEW OF I2C ADDRESSES 182
CREATE OWN SKETCH FOR READING I2C
ADDRESSES 183
I2C MULTIPLEXER 186
PRACTICE BEFORE
THEORY
Let's talk about my approach according to practice versus theory. So
it's good to know in advance how my approach is and how I
structured all of the Project content so that you know what you can
expect from all of the projects. So my typical approach is that we are
talking about the protocols in theory as little as possible. That means
that it's not really a university lecturer where I go into details and give
you tons of slides with all of the aspects. I'm just focusing on what is
really necessary, what we have to do for the protocol and for all of
these contexts.
Then we're jumping right away into coding and we are building the
sketch step by step. That means I'm not copy pasting anything. I'm
developing step by step. And I tell you on each step what I'm doing
and why I'm doing this. Then we are uploading the codes and then
we're making the analyzes with the logic analyzer to inspect how the
whole protocol is set up?
Can we refer the protocol analysis back to the sketch and also from
the sketch to the logic analyzer. And then also we investigate and
inspect that data sheet so that we can also calculate from the pulse
view analysis that, module data or the temperature data, with the
calculator. And also, these bits, we want later on to get out from an
outer, you know, with our own code so that we really understand
what is going on in this protocol, in this module. So combining
Sketch logic analyzer, datasheet, and then back to the sketch writing
our own libraries to get out the data.
DIFFERENTIATION FROM
WIFI, BLUETOOTH ESP
NOW
Let us make a delimitation here. We're focusing here only on
hardware protocols in the Arduino environment. That means
primarily we are talking about UART, we're talking about SPI,
OneWire and E2C and those four chapters we are covering. And of
course there are many more like USB, Ethernet, JTAG etc. But this
will not be covered because the main protocols in the Arduino
environment are those four which are covered here. And also the
whole Bluetooth topic, so the wireless Wi-Fi, also ESP-Now and
ZigBee for example.
They are necessary but this will be an extra Project because the
wireless protocols are much more, I wouldn't say complicated, but a
little bit more complex in the way they interact. And therefore this will
be a separate chapter and this will be just focusing here on the
hardware protocols. That's also the reason why we need a logic
analyzer so that we also understand the basics in this Project and
can then write our own libraries.
OPEN VSC PLATFORMIO
PROGRAM IN ARDUINO
IDE
In this project I would like to show you how you can use my code
from Visual Studio Code and Platform.io to the Arduino IDE. We are
just using here source codes to get data out from the sensors of the
Arduino. And this is our main focus. So it doesn't matter which IDE
you are using. I am using Visual Studio Code with Platform.io
because if you are doing a little bit more with coding and Arduinos,
then you will like those components much, much more.
Because the Arduino IDE is OK, but it will lag in certain times.
Because one, and although I am really a Linux fan, Microsoft did
some really great work with the Visual Studio Code. And this is also
open source. And if you have some issues with sending here
telemetry data back to Microsoft, you also can google the VS
Codium. This is because Visual Studio is an open source program.
They are also open source without any Microsoft attachments. But
Visual Studio Code, which I am using with Platform.io, is just a great
device for coding with the Arduinos.
Give it a try.
All of this content should work right away. If there are some compiler
errors it could be because Visual Platform I.O. and Arduino IDE have
some different kinds of C++ compilers. Then there should be some
minor changes.
But often the Platform I.O. is more stricter than the Arduino IDE. For
example here I have to set up some function prototypes or I have to
declare the functions before I use them. In the Arduino IDE that is
really a topic and doesn't matter. So for example if I have my
function here then I could use this function before it really exists. And
this is something that doesn't work in the Platform I.O. for example.
So as you can see here the code is working right away. And this is
what you can do to transfer your code.
Another option is you take the CPP, copy to a new folder for example
and then you're changing the context to INNO and then you can
open it also with the Arduino IDE. As you can see, double click on it
and then it should be opened. Need a little bit of time. Then I get
here a prompt that I have to change it into a folder. And there we go,
we have it. Deleting the Arduino.h and that's it. So this is how you
can easily transfer the code from my Platform I.O. to the Arduino
IDE.
DIFFERENCE OSCILLOSCOPE
VS. LOGIC ANALYZER
In this project, I would like to show the main differences between an
oscilloscope, which is on the left side, and a logic analyzer, which we
have on the right side. And as you can see here, I have this tiny
device. And the main one of the main advantages of this device is
that price. It costs you around €20.20. It depends on where you get
it.
I bought mine for around €12. An oscilloscope is really a big
measurement device. It costs you a couple of €100, it depends on
what you need. And we will dig a little bit deeper later on in the next
project about all of the features of on logic analyzer. But to give you
here a short summary, we have here 8 channels, which is completely
sufficient for us in the Hopi area.
And with an Oscilloscope, of course, you have to watch how many
levels you would like to capture. But what is now the main
difference? The main difference is that we can measure with an
oscilloscope analog data. That means you can measure on your
circuit, for example, how is the voltage between and diodes. How is
the voltage or some, references according to time to the voltage in,
upside and and capacitor.
So it's more accurate, and you can granulate your information in
much more detail. On the other hand, the logic analyzer is only able
to convert the voltage into lead levels from 1 to 0. That means when
we have 3.3 volts or 5 volts, which we normally get from our early
inos and ESPs, Then this will be shown as in 1. And if we have no
voltage, then it will be shown as a 0. And this is also what we talk
about TTL.
This is the transistor logic level. And in this case, our logic analyzer
is only able to get series and ones converted from our spectrum from
the voltage. And here, we can measure, of course, also this kind of
logic levers. but much more. And you also see more analog values
as well.
So as you can see, much more detail, much, much more I would say
is a little bit complicated, but it's a good handheld and good
measurement device if you want to dig a little bit deeper in the world
of electronics. What we are focusing on is investigating and
analyzing all of the different kinds of protocols, and therefore, a logic
analyzer will be here our best advice at a really good price. And later
on, we talk about the features. The main difference is that you can
also buy a logic analyzer for €10200, and then we'll receive the
specs later on.
LOGIC ANALYZER
CHARACTERISTICS
So before you buy a logic analyzer, you go through your mind and
think, which one is the best one? Of course, always a cheaper one if
it works efficiently. But there are main differences, and I would like to
go through all of them so that you have a good overview. What is the
best logic analyzer for you? So the first thing I think is one of the
important points is the sampling rate.
The sampling rate tells you how quickly the logic analyzer can
capture and store digital samples of the signals that we are
analyzing. Here in our case, we have 24 Megahertz. And in my
opinion, this is absolutely enough for the hobby area. We can
capture e squared SPI, UWART, One Wire. Also, some non standard
sized for DHT and temperature sensors, etcetera, works absolutely
fine and accurately.
And higher sampling rate, allows you a little bit also more precise
capturing and fast changing signals. So the number of channels
indicating how many digital signals can be captured simultaneously.
And a logic analyzer can hear a very bright range. So now you have
to think, okay, my ESP, etcetera, the adenos have much more and
megahertz, maybe also the protocols. have it.
But as I told you before, with those protocols, which I mentioned, we
have 24 Megahertz here. Absolutely. No problem. The number of
channels. So here we have 8 is also absolutely enough because the
maximum we are using in this Project will be, I think, 5 channels for
SBI.
The other ones e squared c, u r does etcetera only needs 2
channels, one wire. Of course, one etcetera, and also this non
standardized, I think also two channels. That's it. Plus we don't need
a normal hobby area with the otherinos and ESPs. Memory depth.
Also, the real time analysis and protocol decoding, these are all
software related issues in my case. And we will go through all of
these points, but, this depends on which software you're using. We
are using, and we come to this also later on, open source software,
which is available for Mac OS Linux, nearly every distribution of
Linux, and, of course, Windows. And therefore, it's easy to use, free
to use, and we really use them. But I would like to show you here
also an auto variant.
But if you take a closer look at the license of this software, you can
see It's, attached that you have bought an original cell geologic logic
analyzer. And therefore, we always conform regarding the license.
Therefore, we are not using this as we are using the open source
software. And also because we can also have an external decoder,
and external decoder is really an important point because, with
Python, you can adhere to your own programs and can,
implemented in sick rock, pulse fuel. One point, maybe this is also
something for you. This is a really, really, cheap product, and you
see it in the whole handling, etcetera.
It's not very, really durable. And I think in a few months, etcetera, this
will fall apart maybe, but maybe not. And as you can see here, this is
just a good product design. So if you like product design, etcetera,
then maybe you should avoid those cheap ones. If you are just
wanting an logic analyzer, which is working and is cheap, then go for
example from asset delivery.
PRACTICAL USE CASES
AT A GLANCE
In this project I would like to give you an overview of practical use
cases where you can use your logic analyzer in a hobby area. And
we start with, of course , understanding. We often use some external
libraries to get data from a sensor but do we really understand what
is happening there? And also if we want to use sensors etc with for
example an ATtiny or an ATmega where we have limited resources
then it's often the case that we have to capture the data by ourselves
because the external libraries are too big.
And therefore it's a really good way to understand once you have
understood the principle you can fetch the data on your own. And
also the signal capturing that you are able to know what is a logic
level, how you can transform a logic level into ASCII code into real
life sensor data etc. And this is really interesting because it gives you
a deeper understanding how all of this ecosystem is really working
together. Trigger connection.
Really fascinating point because often when you are searching for
some errors in your electrical devices etc then you have to know
when something is triggered and therefore we could also make an
example later on that we start the measurement when something is
triggered. And then you can see okay in this time area. This certain
package will arrive then I have to change something etc and this
gives you another perspective of the whole conditions of what is
going on on your circuit.
And I would like to start with the first concept and this first concept is
the AND. And it will be used with the sign of the ampersand. Don't
mix up the logic AND with the Arduino environment. So if you're
using an IF condition you have the first condition and a second
condition you're using two times this ampersand. At a bit level we
are only using one ampersand. And the AND condition of the bit
level means that we are having two numbers and each of these two
numbers have to be true and then the result is also true. So let's
check it in a truth table.
I'm sure a few of you know a truth table. It's some basic concepts but
you may do it also in school. So we have here a number one then
we have a number two and this is our result. So now when we have
the first number and the second number these are two zeros and of
course our result with the AND is also zero. If we have a one and a
zero this will also lead to a zero. The same exact example is if
number one is zero and number two is one then we have a zero.
And now you see the last example is if we have two numbers one
then it also the result is here one. So this special case is absolutely
necessary that we understand because why do we need such an
input? Because we want to mask some numbers here. What do I
mean with a mask? So for example if you have here a number I
store it in the variable x. So just an example: some binary value what
we get from a sensor for example. So it's a 0 1 1 0 and I have 1 1 0
0. So eight bits is one byte.
This is a typical order of what we are doing later on in all of this
evaluation. And now the case could be and this will be exactly a
case. I think in the DHT20 that we only want the last four bits. So this
one should be deleted , this one and this one. And we do not do any
string operations here. We make it in a mathematical way and
therefore we could say here our second variable and we are
indicating in the Arduino environment with a zero b that now a binary
value is coming. And then we say four times a zero and four times
the one.
So let's see what I want to do here. I'm writing here those numbers
once again so that it is really really clear what I'm going to do here 0
0 0 0 1 1 1 and 1 so now the idea is so let me go here a little bit up
that my face is here something covering as we did here in the truth
table if we have 0 0 then it's a 0 0 1 0 0 0 and now everywhere
where is a 1 in our original number there will be also a 1 so that
means the last four digits will be 0 0 1 1 0 0 so what we did now is
we are masking out the last four digits by making an AND operation
with these binary level so in the Arduino environment we are doing
later on x and y and the set value is 1 1 0 0 why is that so because
the leading series doesn't need has any values so we only have here
the last four digits and we are copy the digits because here we have
the four ones and with this operation by each single digit... digits
what we want and this is exactly what we are using later on in
evaluating here our sensors one special case when you when we're
investigating some libraries we see another variant of this so for
example we see here some hexadecimal values and this 0x
indicates there's an hex value coming and 0f means that we... f
means
And now I would like to shift here to the left. with those 2 signs. So y
equals to X, then I would like to pitch shift to left and how many
spaces I would like to shift. And for example, I would like to shift four
times to the left. That means now my new y level could be, 11
010100.
And now four times the new is wrong because we shifted it to the
left. A few things to that. We have to change the wire here. Now to
binary. It should be long or something like that because a binary can
only hold 8 bits.
So if you're not changing the data type here, we are not adding 4, or,
a pan tier 4 of these zeros. because this will be shifted to the left. So
those 4 will be deleted, and this will be the new sign. But we want to
have a long because the practical example here is that we're
combining 2 registers from ABSENCE. So for example, we're
reading the first eight bits, then the last four bits sort of only half of
the and register, which is in the DHT 20 example.
And, therefore, we only need the first four bits, and this will now be
our new number, for example. And this, you can also write another
variable so you could say for example, x, bit shift, 4 times, and then
we could use an or and I would like to add here those 4 values. And
now my set value could be Zed equals 1101 011 0, 0, and now the 4
last digits, and this will now be my new number with this expression.
And this is exactly what we are doing later on in our evaluation. So it
means x, pitch shift to 4 so that we get those values.
And then we're making an or with those 4 digits and adding those 4
digits with this operation to this number. If it's an example, we have
here, of course, also the second example with shifting it to the right.
same principle, a little bit different in the output. So let's see. We
have here our x And we have 0101, and we have, yeah, 4 of the 0s.
And now we are seeing our y variable is x pitch shift to the right four
times So what the result will be now is 0000.
The new 40s And then adding the number here. So what have we
done now? We have shifted those 4 to the right, that means they will
be deleted. Nevertheless, what kind of data type we have. And those
4, we are putting here into the place of this one.
And those 4 will be here. And this is exactly what we're doing here,
and the result is now because we are deleting all of the series in
front because they have no value. And then if it's 1, 2, 4 equals to 5
in the 10a, 10 potential. Okay. So now we are also aware of the
concept of bit shifting to the left and to the right.
We also have some use cases here later on. especially this one will
be used because this one, we're using later on also for deleting
some numbers, etcetera. And now we can use it also later on in the
arena sketch.
PRACTICAL EXERCISE
EXAMPLES 1
Let us start now in the practical part. I'm using a platform you own
with Visual Studio Court, and if you like the audio ID, that's
completely okay. You can use all of my codes also in the audio ID.
I've made a project on how you can transfer the code. And now we
can start with the bit operations.
I'm using an Arduino Uno and clone it. And this is where we execute
the codes now. And now we are starting here in the setup part. We
don't really need any external code because I only want to use it in
the setup because the code should only execute once. So now, we
start with the first example, and we are creating the co step by step
together so you can follow me along with all of the examples.
So I'm creating here a new variable in the data type with byte, and
we are indicating that this is really a byte with the 0b. And now 8 bits
can follow. And for example, we are saying here the following
number. So this one with 1000 in the first four bits and then 0 111 in
the second 4 bits. 4 bits are also called nimble, and therefore, it's
common, really common to, to use half of the bits also.
Yeah. Okay. And then I would like to show you how we can print out
such numbers. So for example, you could say print And this is also
what we are using later on because I want to print out, what is the
binary value. And, also, I would like to have the same value in hex as
in decimal.
And what will happen if I'm not indicating here the second So then
let's upload the quote. And what we got, we the handler operations 0
prints with the baud rate 115200. Then I got my binary value as we
type it in on line 22. Then I got the hex value, the decimal value, and
also a decimal value because if I'm not prompting here, second part
of the, parameter of the function, then I will also get the decimal
value So far, so good. We are using this example very often because
it's a kind of debugging and I really like it.
So, therefore, just commenting out all of the lines before so that we
go on do not get an inquisition about the reused variables with
control and the hash example, you can make here in Visual Studio
Code, commenting out many lines at once. Then I would say we
start with the first and and or example. So for example, we have here
invite x, just with 3 bits that we can see here in the example, 111 And
now I would like to make here an, yeah, for example, we make here
value 1. and value 1 equals to X And Y, and then we copy this 1 and
say byte value 2, is x or y just to see what happens if we're printing
out those values Then we say well 1 in binary. Copy this 1 and say
well, you too.
So just before we hit your upload, we think about what will happen.
And if you can reproduce the result. Then we upload the codes, and
we're getting out here, halibut operations, and only one radio. Why is
that so? Have you expected those results?
And, of course, it's an all operation, and we have here on both sides,
and each digit on 1, and therefore, we got here the result 4 times 1.
Nothing that we don't expect. So now I would say we are trying to
solve the wise operations, what we did in our theoretical parts. And
therefore, we are starting with the new x. So let's indicate here the
example, pitch shift, the byte x 0 b, then I copy here the values.
This is the value from the left side. Now I would say the byte y is x
times 4. So we do it, firstly, in the byte way. And then I would say
long y2isx bit shifting 4, and then I say long. set is x, but this is also
an example so that we could do it in one term.
Why is that so? We have here the data type bytes. And this is our
saved X And now we are shifting four times to the left. That means
we are creating here the value 0000 and shifting all of them to the
left. But in and by it, it only has room for 8 bits.
That means that those 4 values will be deleted, and we only have
those values. And the first 0 will be deleted, then I have 1, 2, 3, 4, 5,
6, is the correct 1, 2, 3, 4, 5, 6. And this is the reason why we have
this value. In the second example, I changed the data type to long
and I shifted the x to the left with 4 digits. And as you can see now,
we have exactly the number, what we have here, but also the 4 digits
that we added to this number.
And this is what we are using later on because we want to have the
first register and the second register definitely had to make the
space that we can add the second number because it's different.
Different approaches. Maybe we should add 2 registers to each
other. Maybe we have to combine it. This is what the datasheet tells
us, and therefore, we're going through here.
SpaceX. And the third one is all in 1. That means we'll make a bit of
a shift in and long. 2 for, 4 bits, and we made an or we made an or.
That means the last 4 digits are those values and the first 8 digits are
those values.
So let's focus here. Can I place it a little bit up? I don't think so. So I
have to zoom out. So, now, okay, pitch shift to the right.
That means we have to use it here. Bit shift to the right. Our byte x,
for example, is 0b, this one, And now we want to have the byte y is
x, pitch shift to the right four times. And now we're printing out our Y,
0 print line Y in binary. Let's see.
And as our calculation was, let's do it a little bit on the big side here,
we have shifted to the right. That means all of these will be to the
right and replaced with 0s, and all the 0s are deleted because there
is no value inside. So we have 010, and this is exactly what we get
here out of our operation. And now I would like to give you and show
you an example what is really also common when it comes to
evaluating, the values out from and sensor. because often we are
using the output with serial read or with e squared t read, and then
we are saving the data inside an area, and then we are fetching and
working with the area.
And the low byte, for example, is in the buffer 1. So how can we
know what should be the result now? The result is high and low
bytes together. So not operations is the first value and the second
value. This should be the result.
And how can we do that? So for example, I could say long value is
buffer, 0, and just hold on here a little bit and try to think how you
could create those numbers with Bit Operator. because when you
think on your own, it's much more that you comprehend all of the
concepts. And now what we are doing is we are creating space. We
are shifting eight spaces to the left because it's long, We are not
deleting everything.
We are appended, and then we are making a logical or with the
buffer size of 1 because this is the low byte. And when we are
printing out now, the value of value in binary and also in decimal.
This could be our desired value, what we are getting from a sensor,
like the pH 1750, 1750, I think the light sensor. So this is one before.
We don't need this one.
So this is now our combined value. As you can see, the first two are
printed here. This is this one with a high bite. And this will be,
indeed, tomorrow, those values from, 12,305. And this is a way how
we can access data from 2 or 3 or more registers in an 8 bit space, 1
byte space, and we can then have higher values like those one, as I
show you here.
PRACTICAL EXERCISE
EXAMPLES 2
And as a last example, we of course want to check if our theoretical
masking was right or not or correct. And, therefore, we start with
spike x equals. So this, I would like to copy and paste because I'm
not really good at typing the serious ones. So this should be our first,
is that right? Yes.
And here 0011, this is the y part. Then can I zoom in here with yes.
So now I would like to create the byte here. The set is x and y, and
we're printing out 0 print line zed. And let's take a look.
But before we do that, I would like to show you the second variant
that is called with an 0x at those values with the hex value 0f. And
now we are creating here. Those values should be exactly the same
result as we did here. Let me see. Here we go. here with the binary
value so we can replace it both.
Then we have some ores left here. Therefore, are they the same
values? No. Biotech is 01101234. and also he applied for 1111.
Set is xory And then we have a serial print. There we go. and tack
and here, our pie, oop. I missed this one, and now also here and
byte is missing, but now it should work. Then upload the code and
the result should be an 11.
It's not correct because I missed something. I have to enter the 0b in
front because otherwise, it will be converted from an integer to
binary. And now it's a binary value, and now we should have, yeah,
the same exact results as before. And this is what we calculated. Not
really because I made a mistake here.
but now the value should be as here. Now it looks the same. Yes.
Okay. An error in and the byte.
y, but now we have the same examples. We have to check if our
theoretical parts are correct, and it is. And now we are well prepared
because we did a lot of, or a few examples in the audio environment,
and now we can jump right into Paul's view.
WHY PULSEVIEW
We started this chapter by discussing why we should use pulse fuel
from sickrock. And one of the first points is it's open so I really love
this whole fuss philosophy, free and open source. It means as you
see, you can see in the license, we can also use it for commercial
purposes. And, also, we can add our personal programs and
decoders to it. But to this, we'll come later.
And then what also is really, really necessary that we check on is the
hardware support. If you buy a logic analyzer, keep an eye on if the
logic analyzer is covered here. In my case, this is the ACET delivery.
You can get it on Amazon for really a few bucks, and it's covered
here. But, also, all of these cheap Chinese clones from eBay,
etcetera should work because there is the same chip inside, but I will
show you how you can, choose the right hardware here in Palmsville
later on.
And I think a lot of these loans have the same chip inside, so you
can follow me along. But also other and expensive ones, like the
sallyas, etcetera, and This is why I really like it here in the past year
because it has a broad understanding and support of hardware.
Yeah. And also the community is really good. As I told you before,
there are a lot of external decoders which you can find in GitHub,
etcetera, and implement 2 year projects and have fun with analyzing
different kinds of protocols.
DOWNLOAD
For the sake of completeness, let's go through where you can
download all of the things. So on sickrock.org on the left side, there
are downloads. And here, you can find a different kind of platform.
For example, for Linux, you get an app image for Windows and
normal Instala on for Mac I think also an installer and container. So
here, you see some notes.
So the first links are somewhere broken or what else, doesn't matter.
Just use the above, the below 1 of the pulse view 0.4.2. It works also
the same as the newer version, and they're good enough to go. If
you install here are some, as you can see, a few more inputs about
all of the binaries. It's that, but just install the container here and
you're ready to go.
For Windows, I just installed it. Just clicked through the installer.
There's nothing special. And you can see here this is the Windows
version and this is the Linux version with the app image. I have
exactly the same features.
So nevertheless on which platform you choose and what working,
you can do it, the exact same things as I did it. And also here, just
switch to a talk stream, talk theme, but all of the rest is the same.
BASE SKETCH FOR ALL
EXAMPLES
In this project I would like to introduce you to our base sketch. The
base sketch will be that code that we are using for every other
project and this will be our initial starting point where we code
together and build up step by step our code. Therefore we have here
just a basic one and we are using the platform EO with Visual Studio
Code. So if you are with the Arduino IDE then take a closer look at
chapter one where I explain how you can transform my code to the
Arduino IDE so that you can follow me along with the given code.
So I start with including the Arduino.h. This is the library that is
necessary that you can use, strings etc. for the C++ environment
and especially for the Arduinos.
And I am defining here a pin, for example pin 8, because our main
purpose in the next project is to make our first measurement with the
logic analyzer and therefore I just want to make one pin on a signal
output and then we will measure those pin 8 for example.
But those pin 8 could be any kind of pin, it just matters that we are
using a pin mod that is an output, setup finish that we know, ok setup
is through and this could be now our pin 8 that we have here and
digital high and digital low for around about 300 ms and exactly this
signal is our main purpose for the next project.
CONFIGURATION AND
CONNECTING LOGIC
ANALYZER
Now I've uploaded the codes to the Arduino UNO and what I would
like to do now is, so let's switch here to embed a few, I have here an
extra jumper cable because it's easier for us to grab the signals. So I
have pin 8 here and I also need a ground here. Then we can go to
our logic analyzer and with the logic analyzer I need the first one is
the ground. So ground is always necessary in each measurement so
that we have the same potential.
So just grab this one, be aware that those two don't get to each other
and then channel what you would like to use.
I have a few more channels here so I grab one of the next and
connect it to the Now I can connect my logic analyzer as well to So
let's see if we have a little bit more cable here. Two wires are
connected to the logic analyzer, one here and one here and what we
do now is we're switching back to our PulseView and in PulseView
now, so let's turn on, we are seeing here for example on the left side
new window, new analyze window.
So new session if you have another language you can change it
here on the general settings and what we want to do now is the first
point is we click here on Saleae logic because we want to use here
another device and for the main part with this cheap logic analyzer
we can use here the FX2LAFW generic driver. Click on it then scan
for the device and here we go. Here stands Salea logic.
I don't know why but I'm using it and now we are ready to go. That
means our sketch is performing right now. We have connected our
logic analyzer.
We can see it with the red dot and what we can do now is we have
to change both settings above. That means I would like to sample
now one gig sample and I would like to have here for example 12
megahertz. That should be fine. Then let's click on run and let's see
if we got here some output and as you can see I have obviously
attached the signal to D1. Then I click on stop and here is my 300
millisecond signal.
With the mouse wheel you can zoom in and zoom out and with this
red clamp we could say that I only want the channel where I have my
signal and this is in my case D1. So now I have only my signal and
what is also very convenient is the measuring tool. Show cursors
and now I can grab the left side and the right side and can for
example measure here the time where there is no signal so a low
signal and here you can see 300 milliseconds. If I push it to the high
signal it should also be 300 milliseconds.
We got here the 300 milliseconds which we have here defined in our
loop. So pin high 300 and low 300 and if I would like to have here a
shorter time frame I could change here so let's get rid of that one gig
for example to one amp sample. I click on run and then you can see
I only get here a high signal so I need a more sample rate 10 m to
get here more of the signal out and this is the reason why I would
like to change here always at a minimum for one gene that I have
here and write a wider span for capturing the data.
The change of the megahertz is how fast the signal will be captured
and if we have a signal which is very fast and we choose here in our
measurement device a very low capturing signal then of course the
whole capturing process will be not really reliable and therefore it's
necessary that we often go above that what it is sent so that we can
be sure that it is captured and this is normally round about 12, 16 or
24. I often give it just a try and click on run so this is also always my
first purpose, try and error and see if something comes out.
So this was our first test now with the logic analyzer and the first
settings and that's it nearly all of these settings are used later on
also in all of these examples. We can use also some external
decoders or protocol decoders click on add protocol decoder and
here we have a lot of different predefined decoders we are using
later on for example the UART and just by clicking on it we have
here in second channel and can make all of the settings here but we
are using those settings a little later on in the chapter UART 1Y etc.
This was just a first glimpse of the logic analyzer, how it works and if
the connection is right with the PC and the Arduino UNO.
SAMPLES AND MHZ
SETTING
Let us discuss in this project once again the concept behind the
sample rate and also the time period what we can set up in the logic
analyzer or in the pulse field. So our logic analyzer is now connected
and I would like to change here from 1G to 2G. What is the main
change here? And if I keep the mouse over the setting you can see
here that I have a total sampling time from 167 seconds. If I change
it to 1G then of course this will be reduced by half and I only have 83
seconds. And this is the first concept. How long would I like to
capture my data?
And the next point is here the sampling rate. And this is a really
important concept because the sampling rate of a logic analyzer is
the number of data points that we would like to record.
So that means if the sampling rate is too low, for example in kHz,
then fast signal changes can't be captured. And we will do this
example later on. That means if we are sending data on the serial
monitor or sending some ASCII's and we change here to some kHz
setting, then the logic analyzer is not able to capture all of the data in
a reliable way.
And therefore it's always a good point to use something in the
spectrum of what the Arduino Uno is, a little bit above, so that we
can be sure that the logic analyzer can capture all of the data. So
here is our example, so let's try it out. If I use the first 20 kHz here, it
doesn't matter because here we have 300 milliseconds. This is of
course a signal that I can capture here.
But with faster protocols, E-squares, C, SPI, etc., it's really
necessary that we are changing here according to the specs to the
Arduino or ESP, so that we can then have a reliable signal. So often
when you get here in the signal processing and you find some
strange values, keep in mind if you have also changed the sample
rate.
FREQUENCY AND
MILLISECONDS
In this project, I would like to discuss with you the concept between
frequency milliseconds and what is the relationship to it. because
often, we can find in data sheets, the term and the data frequency,
and not the milisack. And also when we want to calculate or find out
the right bar rate, it's necessary that we can recalculate from
milliseconds to frequency etcetera. And therefore, we can use here 1
Hertz is 1 switching cycle per second. And 1 switching cycle is
defined you can find, for example, this is one data process, etcetera.
So, in our example, that means, Let me see here. Yes. 200
milliseconds, but we use 300 milliseconds. So just assume we had
200 milliseconds. is 1 5th of a second.
And with the basis of 1 hertz is 1 second, we can riff or transform it
to 5 hertz is 200 milliseconds. And this is what we keep in mind. So
when we are using some analyzing techniques, then, of course, you
can now use the Hertz data, or you can also use here the
milliseconds data. And when we are switching back to our previous
example, we had here 300 milliseconds. And you can also find here
the data 300 milliseconds and also here the value of the Hertz.
So let's reduce. So let's reduce the cursor here and go to the left. It's
around 200 milliseconds. That's good enough, and you can find here
the 5 hertz. And, therefore, it's easier to recalculate, to transform it
so that we know how many seconds do we have here in our
capturing data?
BAUD RATE
Let us talk about the bot rate. And I'm sure all of you have already
used the bot rate or the settings, but are you aware what the bot rate
is? thread is. So one part is the speed when one symbol is
transmitted per second. An example.
But with four words per seconds or let it be 2 words, then it's easier
for you to understand, but I need more time to transmit all of my
content. And this is the same with the bot rate. And normally with the
ESP cert the 2 or with the Adi and Uno, we have some predefined
alterations. So, for example, for Uno, we often use 9600. That
means 9600 symbols could be transmitted per second.
And you might have mixed up the pulse, right, with the serial monitor.
In the end, you saw just some squiggly lines or some mixed
symbols. And this is because the serial monitor and the Arden Uno
have a different bot rate, and then they can't communicate because
you have to be here at the same bot rate. that the sender and the
transmitter can really communicate in the right way. And the portrait
is also necessary for us to understand Okay.
The ESPs. So the 2, for example, have, normally, we're using the
portrait 1152, 0 0. And there are a lot more different portraits. And we
are using, different kinds of, the portraits that later on, for example,
you are or in the E squared C chapter. And we are also trying to, and
this will be also covered in the UI section.
trying to measure is, and then we try to recalculate what is this, what
kind of boundary is this? Why do we need that in some, logic or error
inspecting or when we have an external PCB, we are capturing data.
And then we want to use this data, for example, with an outer
uniform. And therefore, we have to know what part rate is this MCU
communicating or this sensor. And of course, we can take a closer
look also in the datasheet.
But often, we only can capture data and therefore, it's necessary that
we also can recalculate from the time which power rate it is.
PRODUCE AND
MEASURE PWM SIGNAL
In this project, we want to analyze PBM signals. PBM stands for
pulse with model and it's used in the arduino environment for
simulating analog output with digital signals. And therefore, it's a
perfect use case for our logic analyzer to test the logic analyzer and
to inspect such PBM Signers. And we are using our basic sketch
here, and I will edit it here. the base sketch.
And therefore, we say, for example, the PBM 1 is on the pin. Let's
see. We are using pin 5 because it has to be a PBM pin, and you will
see it with this squiggly line. on the board, then we have PVM 2, and
we would say here in digital output. It's on the 6th pin, and we have it
here on the 7th pin.
Then we say in the output, of course, in the setup, we have to also
output all of these 3 signals. pvm1, pvm2, and the teacher. And now
in the loop, we are making a delay here. And normally, we are
skipping delays because they are really, really bad because of the
breakup or whole loop. But in this case, we can use it.
So let's see. we start and we end with some 500 milliseconds delay.
And then we could say, for example, analog. Right? And if you're
doing this with an ESP32, you have to include an external library
because in the standard core, There is not really an analog write
function implemented.
So analog write, PBM 1, and I would say 20. Then I will explain in a
minute what we are doing here. So now it doesn't work. Analog write
PWM tool. Here, I will have a duty cycle from roundabout 200.
And then I made a digital write that we can compare these 2 PBM
signals, and I would say a digital higher. Then I will copy all of that.
We're making a delay of 500 pasting in this block, and then we say
0, 0, and low. So what we are doing here in this loop, we are
producing here 3 signals on 3 different channels. 1 the PBM 1 would
have a short duty cycle by 20.
Then we have, bigger duty cycle by 200, and we have a digital signal
that is the whole duty cycle, of high. Then we have a delay from 500
milliseconds. Then we put all three channels low, and then all again,
it started. I think, yeah, we'll let you know. So it's one second I could
do here.
So then, I keep the last one. So there's a little bit more delay
between then uploading the code. And then we switch here to this
view. And I've attached 3 jumper cables here. on pin 7, 6, and 5.
And now I'm using my logic analyzer. The ground, of course, is
already connected as we did before. And then I'll see if I find channel
1 here, this should be channel 1. Channel 1, get the first Then
channel 2 should be the gray 1. This is the 2nd PVM with 200 duty
cycles.
And channel 3, we are connecting channel 3. Then let's see if I've
connected everything right because we can change now to pulse
view, making a new analyzer. been done selecting our device. Is it
connected? I don't think so.
It's connected to the USB. Then we click on the connected device.
We are setting up ethics, scan, yes. Then I'm setting the level to 1 g,
and we're saying, for example, 16 Megahertz. That's it.
We don't need anything more. Our audio is running right now. You
can see it here on the Also, the logic analyzer is working. And now
we click on run, and we're capturing here some data. Then we click
here to stop.
And I can get rid of 7 to 3 because we don't need it. This is starting
by 0 and the logic analyzer starting by 1. So it's a little bit of, different
name. But, let us focus now on one of the signals. First of all, I would
like to capture and measure what is the time between the signals.
And before you measure, think on, according to the codes, what
would you say? how much time is between one signal or one loop.
And we have here one second, 1000 milliseconds. Why is it so?
Because I have a delay here and a delay here.
Of course, you can only use one delay. But this is just for, and a
thinking process. Then I would like to focus on this one. So let's
focus on the new width. And we have here roundabout 500
milliseconds Also, this is perfect.
So this is channel 3, and this is our digital signal. You see one
straight, high signal. And when we zoom a little bit more in, now we
can see here our PVM signal. and let's measure the first signal. And
this should be it.
84 microseconds, and we have 20 here. So 20 is the duty cycle. We
have in the summary 255. So one byte is 1 digital high, and I only
have 20 from this 255. And therefore, as we can see here, we have
here this little aspect of one duty cycle.
And one duty cycle is round about this one because we have 200
here. And in the second channel or D1, we have 200 in the duty
section. And this will be 1, duty section with 250 5 units. So what we
did here is we skipped the first part and had only 200 from the range
of 250 5. That means if we add here an LED with a resistor, the LED
will not have his full voltage.
And, therefore, the light will be reduced. And even more reduced, it
will be when we are, attach it to the D 0. And this is a typical way we
can control motors or we can control LEDs or some other lights with
PVM because the voltage or the whole supply will be reduced by just
this tiny pulse. And as you can see in the D 2, we have a complete
high level through all of the time, and this is the normal, high signal.
And here, we have this 200 and this 20 PVM.
And in this project, I would like to show you how we can measure
here signals and also that we can produce it from the other inu and
what we are able to do with this logic analyzer.
TRIGGER ON LEVEL
WITH IR SENSOR
In this project, we talk about what we can do with external triggers.
And we often use external triggers for Paul's view when we are
inspecting or investigating our circuit and they're, you know,
searching for some errors. And for example, if we want to test and
turn the system and want to see if the voltage which he passed
through is correct or not correct, we can also use a logic level, and
therefore, some triggering measurements would be nice. And we do
this in this scenario with, infrared sensor and hw201. And we are
simply connecting here the 3 word ground and to unpin, but this pin
is not really used because we don't attach any sketched to it, but we
could do it.
And, here, you can see my setting. So this is the sensor, the infrared
and infrared sensor. And I connect the ground VCC to pin 3. Ground
for the logic analyzer is on an auto pin. And here with this clamp, I
directly connect to the breadboard or to the module to the out point.
And now, we can open and pause the view again. Start a new
analyzing window. Then we click on devices, and we select our
device. Okay. I have attached it to channel 1.
Here it's channel D 0. Then I'm changing to one gig and, for
example, 60 megahertz. So now, let's run. And we, in this use case,
have a high signal here. If nothing is detected, If I put my hand in
front of it, then we get a low signal. So you can see it works pretty
well.
But what I would like to do now is, not that we're getting here straight
the values out of it, I would like to start the measurement when
something is triggered. For example, you could click here on the left
side. And then I click here, maybe I would like to change here if the
flag is changing. Or, as you can see here, trigger on rising edge,
trigger on rising or falling edge. And I would like to trigger on a low
level in this specific scenario.
And then I click on run, and you will see nothing, nothing is captured
until I now put my hand in front of the sensor, and then the whole
measurement will be started. And this is really convenient when it
comes to investigating all of the PCPs, etcetera, or you're circled
because otherwise, you will be spammed out of the capturing data.
OVERVIEW OF SERIAL
COMMUNICATION WITH
UART
We start in this chapter by defining and discussing what is u art. 1st
of all, u art stands for universal asynchronous receiver transmitter.
And this is a communication protocol commonly used for serial
communication. and you are unable to see a serial communication
between a transmitter and a receiver via 2 data lines. That means
microcontroller sensor sensor microcontroller.
And this is very good shown later on also in the policy with the start
and stop bit, etcetera. The parity bit is used to check the data in
credit, and this will be a separate project to play a little bit around
with this parity bit. Yeah. And this was a short summary about you
art, and now we would like to start a little bit more with the practical
part
THEORY OF DATA
TRANSMISSION
One more theoretical project or inputs before we start with the
practical example, I promise. But I would like to start with the TTL
logic. So transistor transistor logic so that we really understand what
is going on here. And I would like to show you here an example,
what we do later on also in pulse view. So, for example, we have the
following measurement.
So, this could be our logic level. Then we have such a thing a little bit
longer on serial. Then we have such a small one. And that's it. So on
this will is now our output, for example, on the pulse view.
And what we have here is, for example, this is our MCU, and this is
our line. We are sending here commands to a module component of
what else. Module. So what we have here is a serum, and this will be
our start. is indicated in UART when we, heavier and high level.
And then with and 0 will be indicated. Okay. The transmission could
be started. Now, we have a high line, and this high line could be, for
example, because this is 2, 2 times measured. So 1 and 1.
Then we have 3 0s here. k. This is not really the same
measurement, but maybe we have three times here. And this two
times And here we have a serum. And then we have one stop, That
means we have 1, 2, 3, 4, 5, 6, 7, 8.
So we're transmitting here the data 1100 0110 because this is the
stop bit. Maybe there's a parity bit, but this will be optional. And this
is exactly what we are doing later on sending data. That means also
that In this time frame, here, this is our data, our transmitted data,
And this is the start and the stop indication. And now, we can also
interpret how long a signal should be transmitted.
And therefore, we could say, let's go a little bit up. we could say, for
example, in an hour, by an early noon, we have some power rate
from 9600 Divided Nope. That was false. 1, I would say it's, in the
other way. So one divided by 9600 should be roundabout.
But I don't want to have all of the data. The last one is 49, so maybe
I can copy this one. Copy and paste it, yes. Paste it here, convert it
and I get Pixlady here out of Pixlady.
But there is an even more convenient way. So we click here on
UART and we go a little bit down. So we have a data bit set of 8, this
is ok. Priority bit is not set. We have a stop bit. The bit order, we
come to this in a later chapter. And here we have the data format.
We can change it to ASCII. And as you can see here, I close this
window. Now we have the ASCII characters directly in PulseView.
Here we have some checksums etc. But here this will be transmitted.
Also in lower and upper case. In this menu we can also invert the RX
and TX line.
This is often used in circuits where we have added some transistors.
So maybe we have changed the logic level here. So that we have
the right logic level. Then we can invert it here in the program. So
now I think we have discussed nearly every step here in this menu.
And also how we can translate UART text into PulseView.
HEX TO ASCII WITH
ARDUINO UNO
And to get a little bit of practice, I would like to show you how we can
send hex data from the audio new scratch to Palsphere, so that we
get the ASCII from HEX. So in the other way around. Therefore, we
are changing our setup a little bit just for training purposes. We are
changing the softness to hardware serial. So, yeah, to KX pin, then
I'm getting rid of all of the software serial stuff.
And I'm adding here an order variable, and it's called unsigned long
previous milliseconds, and I'm assigning here the current run time in
milliseconds. And this is the proper way to get rid of the delays
because delays are bad. They are blocking our whole sketch, and
I'm sure you'll be familiar with the logic blink without delay. If not, just
search. Make a quick Google search.
The AidenO CC site has a good explanation here before. For it. So
here we have to begin. That's okay. Now, we're getting rid of the
delay.
And Now we have if the current mill is, so we have to define it here.
So it's also an unsigned long because it's really a long number. The
current millis is also millis. And if the current millis minus the
previous milliseconds. Greater than.
And if I change the portrait, it also shows, we can see here the mask
is converted. So we are sending here 1, 2, 3, 4, 5, 678 signs from
hex, and this will be translated into ASCII's as well with star and a
stop bit. And this is how we can transfer data from binary hex,
etcetera, to an auto module via u art.
LSB AND MSB
Let us now discuss the concept of LSB and MSP, and we have
already heard these terms before. And it's called least significant bit
and most significant bits. So, for example, if we have the binary
level, the binary number of 11001100 and also n The terms of least
significant or most significant bit refers now to the position and the
significance of bits within a binary number. So what does that mean?
That mean?
For example, if we got this number, we can read it from this order, or
we can get the values in this order, but we have to read it in another
order because it's called the least significant bit. And this depends
on the datasheet and the manufacturer, how they will transmit the
data to us. And when we are switching back to pulse view and click
on UART, we can see here the Bit order. Is it LSB first? or is it the
most significant bit first?
And if you change the order here, we will also see that something
will change in our outputs. And therefore, this is a necessary event
that we know what is our least and most significant bit. And normally,
we are translating our values here. So for example, this is 124, 860,
32, 64, 128, and 200. 56.
And now we would add here on each position where is in 1 at the
numbers, and this is our final number, what we are transmitted. But
always keep in mind in which order we have to read it. And why is
that so? For example, if we have here an register from a module 8
bit, and this is split it in half because here we have the plus minus
sign so that maybe we will get values with and sign. And now also
the second register is split in half.
And now This one, this content is our temperature value, and then
the comma value will be attached to the last four bits and the next
register. So it's not every time it's so clear what kind of information
we get. And we will see this later on, especially in the eastward seas
chapter with the DHT 20 evaluation of the sensor that the datasheet
shows us where and in which order we have to read it. And just keep
in mind what is most at least significant, but keep in mind when you
have some dollars or euros in your bank account, And, for example, I
am deleting you on the least significant bit. It has not so much input
into your life.
But if I'm deleting you on the first number in your bank account, so
the most significant bit 20. This has a tremendous impact on your
life. And therefore, the least significant bit should always be
considered in order to read out the right radius.
BINARY LSB AND MSB
WITH ARDUINO UNO
Let's now see a real time example with all of this LSB and MSP
topic. And I would like to send binary data here. Only three
characters. So my name, Eddie, and these binaries, have already
prepared. So let's switch back to our code from before.
uncommented all of these hex things. And now I would like to send
the least significant bit before as well as first. And now I would like to
send via 0 dot write. And now I'm indicating within 0 and MB that I'm
sending binary values. And my first binary value will be this 1.
So 0, we also, I think, could we use Let me see if we can extract it
here. This was not right. And decoder, I would like to have t x. It's
only the hex values that I am getting here. Okay.
hexdump. No binaries. Nevertheless, I'm typing all of the stuff in. So
let's see the least significant bit ready here. Let's upload the code.
The serial monitor should already, converted to, yes, converted. Now
we click on run because we have already set everything up from
before, zooming a little bit out, and you can see I receive you some
data. Then we click on stop zooming a little bit in, And of course,
we're getting these 3 characters as before. What changed is that we,
not the hex values, we are using binary levels. Now, I would like to
change the order.
So what do we do now? copy this one, then the value from the first
character will now be changed in the order. That means now I have
011 at the beginning, and now I start with the ending. So my new
number will be from this direction, from the, 1. So let's see.
0101. 1 1. Then I have 000110. Then on the second number, I have
here 1, then 0011 0. And in the last one, I start with 1 0010 110.
Then we're uploading, once again, the code. Let's see what the
serial monitors are printing out. If we convert it, no, because the
most significant bit order is now not recognized. Let's see here pulse
view does, assuming we are out, we're getting here the new data,
assuming a little bit in. And now I haven't changed anything.
So you can see the same thing as we have here some data which is
not recognized and the ampersand. And now I'm changing to the
most significant bit first. And the order will be changed. And also, the
pulse you can now read the values. And I hope with this practical
example, it's now clear from which side we are reading the data and
that we can change it also in Pile Sphere or in the other Unity, how
we want to read it.
Hardware Serial with ESP32
In this project, we're gonna try to use the u art interface with an
ESP32. And I've here the ESP32D1 mini, And it's nearly the same as
the development board, just another layout and size of the board, but
you can do it with any kind of ESP32. Be aware the pinout can also
differentiate between the development port or another ESP rum,
etcetera, because often the manufacturer or the company which is
delivering you the ESP, changes the layout. I don't know why, but
there are minor changes. So be aware, especially from asset
delivery, I saw some different pinouts out there.
And when we take a closer look here on this pinout, we can see we
have a serial interface here. And we can find it here on the upper
right side, TXRx. This is our interface 0 because the ESP cert user
has 3 different hardware series. And we can find here the uart 2. So
where is uart 1?
This will be used also for the internal connection between the USB
and the ESP. So this is not available for us, but we can use the
second one here because the With the index 0, it's pretty
straightforward. We can also find the Rx and TX. So let me see. it's
here on where it is?
rxdx. We can find it also on the pin out here. But I would like to use
another kind here, the u 2, and, therefore, we are using the pin
number 17, GPO 17, for our T X communication. So let's jump into
our basic sketch. And what we do at the first point is we are including
the hardware serial library.
And as always, this is in the core from the ESP 30 to include it. So
we don't have to install some external libraries here. We just have to
include And then we're starting with hardware, serial. Then we give
the name of, for example, serial ESP 32. And here in the brackets,
we have to type in which number we want to use.
And according to our pinout, we are using the 2nd u at interface. If
we want to use the normal hardware, serial port, then we have to
enter this 0. So far, so good. Nothing special until now. And now we
start with 0 ESP32 begin, and we can also use here the faster bulk
rate from 115200.
And as you can see, I have a normal serial beginning and a
hardware serial. And then we could, for example, here, type in 0 print
lines, set up finished. What we also can do It's just show you all of
the possibilities. We can say, while, if the 0 and the serial ESP. So if
they are not available because if we use these terms, then we're
getting back here and true.
But if there is not, So if it's false, then we keep here in a while loop.
This gives us the proper ability to check if the serial communication
could be established or not. And if so, we go through the loop.
Here's our link with our delay. construct.
And we want to have here, for example, each 300 milliseconds. I
would like to print out a serial print line on the monitor. Hello, serial
monitor. And I'm the serial s p 32. Should print our handler, pulse
view.
Then let's see. If we have some typos or it's the compiler, yes, we
get the success, then we upload the code. So then we can open up
the ceremony time. And we can see the output from the serial
monitor. And now we are changing to our past view.
I've already inserted my device here. changing here the sample rate.
We click on run. Then we're adding here just one channel. Then
adding in a decoder, it's an URL decoder as we did before.
Then new art that takes because we are sending something. 11
looks good. And I would like to have the ASCII. Then we're tuning
into one of the data frames. and we're getting here Hello, pulse view.
Perfect. So in this project, we learned how we can use the hardware
serial on an ESP32, and we can change the channels or which
hardware serial we would like to use and also that we can combine it
with the serial monitor, and the hardware serial on an ESP32 level.
WHAT IS A PARITY BIT
Now, it's time that we talk about error detection in UART. And
therefore, we have the parity bits. And we are focusing on the even
parity. So what does that mean? The parity bit is a bit that is added
before the stop.
and tells to receive what is the number of ones in the whole
transmitted number, the binary numbers, is, odd or even. The
possible settings for the parity bit are odd or even. So odds the parity
bits are one if there is an odd number of ones in the data frame.
Even this is what we are using, the parity bit is 0 if there is an even
number of ones in the data frame. So let's take an example.
For example, we have the following payload. We have here 1 byte
12345 67. And this is, the parity, the bit. So that means we have in
our data frame 1, 2, 3 ones. And, therefore, we are in an even parity
bit order.
We need to add in the parity bit on 1 so that we have four times on 1,
and that's an even number. Let's take a closer look at the payload to
them. Here we have 2 ones in my data frame. And I'm adding here
an 0 to this is the parity bit. This is an additional bit, and therefore,
we're using here and 0 because we have 2 of the ones already here.
Then I would like to add the serial and also you can see what I can
add here. And the standard was 88 and 1. But I would like to add
here an even for the even priority bit. 1 stop bit and it's 8 bit long. as
we can see here, 8 bits long, and one stop bit.
a lot of data coming in, zooming in a little bit to one of the new data
frames. And I have already entered the parity bit. So let's investigate
if we are true. If we have everything set up here. So, We have 1111,
so no parity bit is here, 0 as we can see in the line.
That's the check. 123456 is also even. He's only 1 So the parity bit
has to add 1 because then we have 2 ones and this should be an
even number. And as you can see, the parity bit is here 1. and,
therefore, we have a positive check.
Let's see here. 11. So the parity bit is 0. And as you can see here, 1,
2, 3, 4, 5, that means an odd number. We have to add here the
parity bit to 1, and we also have an even number.
Very good. And this is the implementation of what we could do with
the ESP32, for example, to add the parity bit to have here a little bit
more control of our data frames if everything is working well. And
afterwards, for example, if we're transmitting it the data from an ESP
or from a module, we could do in the code and check if the parity bit
has an error or not, or if, we have to calculate the numbers of the
ones, and then we can check if the parity bit is here and even or not
number in summary. And this is why we are using the parity bit also
in the code.
UART COMMUNICATION
BETWEEN TWO
ARDUINO UNOS
Let us now make some practical examples. And therefore, I have 2
otherinos, which are connected via UART, and the main goal of this
little project is now that when we are pressed the button from this
auto in order, we're sending and you add a message. This will be
received by the second order, Uno and an LED will be turned on. I'm
clicking the next one, turned off, turned on, turned off. And this
should give us now the knowledge that we are able to send data via
UART, what we already know, and receive data on an ARENA basis.
Yeah. And therefore, I would like to start with the wiring part. And we
have here the 2 other owners. could also be implemented with
ESPs, but be aware, not UNO and ESP, this will be covered in the
next project. So first of all, we have a fivefold and ground to the
button.
We have here a pull down button, resistor And then we are
connecting the button to the GPO 4. 56 will be our uart lines. which
are connecting to 3 and to the 2nd owner, this is the receiver. We
have to connect ground and ground from the auto in owners. And
here, we have an LEDM.
Also, insert a resistor for the LEDM. That's, for roundabout 200
ohms. And this will be connected to this GPO number 6 and to
ground. And that's it. So we are simulating that we are, sending data
and what often is the case that we are receiving data.
So our receiver is now our main focus in this project. So, therefore, I
have it. I have 2 sketches, and I would like to focus now on the
transmitter because this is really an easy one and we already deal
with it. So the receiver is here on the right side. I've already
implemented the sketch here. And I have 2 USB ports here.
So, with the platform you own, don't forget to edit here the USB
numbers and the sender. has now on software 0, all what we did
before, and input for the button. And what I have here is a little
debunking logic. And with this debounce logic, I tried to find out if
there is some flattering with the buttons And if I get a signal under 50
milliseconds, it will be ignored. And if it's really a button click like
here, Then I will wire you out the string button.
That's it. Nothing special. Everything we did before. But now I would
like to focus on the audio on the left side where the LEDs are. And
this is often a use case where we want to receive data from a
module, but often also send and receive, but now we are just taking
the receiver site.
I also did a software serial here. Whispin 2 and 3. This is what we
have here. 2 and 3. Eric's and T X.
So you can see here the Rx pin is on 2. And here, the Rx pin is on 5.
So 2 and 5. Let me see. 2 and 6 are now combined because we
have the receiver to the transmitter and the receiver to the
transmitter.
So let's keep on the receiver module. We have an LED on the pin
gpio 6. We are starting with the baud rate of 9600. This has, of
course, the same value as here. And we're setting the pin mode on
output.
And now we are coming to the loop, and this is what we are, want to
want to do now. So the first step is that we are checking if there is
some if we are getting here some data. Therefore, we're making a
while. And let's see if we received some UR data. And we are seeing
dots available.
And then we are seeing, string uart message, for example. is
serial.read string, and then we're printing out this message. Then
we're uploading this code. And of course, we don't have to use a
serial. We have to use it here. How do you know uno receive 1?
So now we can upload the code. And it's up. Then let's test. receive
a start so that the setup message will be received, clicking one time,
clicking 2 times, 3 times yes, it works. But what we see here, we
have here some white spaces and line breaks.
we can get rid of them by saying, uart message.trim So now we
should only get here the button as in string. And now we can, for
example, say if we receive an uart message. uart message equals to
button. because we are transmitting and stringing. And, therefore,
we can check if this is on string.
And then we make a digital write here. And I also want a serial print
line toggled LED. This will be LEDM and, for example, high or low.
But what I do now here is I make a digital read, digital read, LED. So
what I'm doing here, I'm reading out what is the current state now is
false because it's not on.
And if I get here and false, it will be turned out of So I am transferred
to true because I make a note in front. So I received it here. This is
false with this line, it will be true. So I'm clicking on it, and now it's
true. If it's true, the LED lights, then I get back from the digital read
true, but with this negation, I make it unfold.
So I can toggle the LED with only one line. Okay. Then upload. Let's
see if it works. And we can do this in the next with you exactly in the
same way, with an ESP32.
So we are clicking on our button. We are getting the toggle LED.
Now when I'm clicking once again on the button, this expression will
be true, but with the note, it will be false. So false is now the whole
expression, and therefore, it will be turned off. Let's see.
it's off clicking, clicking. And what I would like to show you in this
project was or is that how we are able to communicate and receive
data. Because often, we want to receive data from an UI module.
This is exactly what we can do here. We also can add here and
equal greater signs, greater than 0, and then, is also in a convenient
way.
how you can fetch and get data from a module via UART. And in the
next project, we take a look on how we can communicate with 2
different logic levels.
NOTE THE LOGIC LEVEL
WITH ESP32 TO
ARDUINO UNO
In this project, I would like to show you what we can do when we
have different logic levels. So, the artery in the Oulu has some logic
level from 5 worlds. What does that mean? If we measure here with
a multimeter, the GPR outage, then we see the UNO has 5 worlds.
Besides that, the ESP 32 has 3.3 volts.
That means we can't not connect directly the GPOs from an Audi in
Uno to an ESP32 because the ESP32 is only able to handle 3.3
volts. So if we are using 5 volts and the GPO from the ESP32, It
could happen that we destroy those ESP, GPOs, or even the whole
balls. So therefore, we have to use logic levels. And this is a
common case because often we have other kinds of voltage levels.
For example, 1.8 volts It's very common with some power cons,
reducing, modules, for example, in GNSS module where you can
use just 2, double a or triple a batteries.
So therefore, we have to slow down, reduce the voltage, and reduce
the power consumption. And when we have want to use UniN USB
32, then we have also used here, leverage And a l a level shifter
works in a way that we are connecting the higher voltage and the
lower voltage and, the transistor on it will then transform, I would say,
the logics, to to the desired voltage And we, can't use directly here
voltage dividers because we want to correlate and want to have the
nuances and the details from our PDM signal, for example. And,
therefore, these logic level shifters are not really expensive and easy
to use. And in this case, we want to use the same example as
before. We want to have a push button. We want to turn on an LED,
and the LED is powered by the ESP32.
And we wire you out. We're sending as before and string. So how
can we do that? We're using his software serial of the auto in the
Uno site. And here, you can see We have a high voltage, for
example, 1, 2.
We also can use the pins we inform. And this is the UART signal.
Here, we have the five ports in the ground from the Uno, and,
therefore, we have not connected or we don't need to directly
connect the grounds together because we do it with the level
shifters. You can see you with the ground connected. And we have
3.3 volts here.
So, therefore, the logic level shifter knows, 5.3.3 volt, what they have
to do. And the UART We're now connected here to Eric's and T X.
This is the 2nd channel of the hardware serial from the ESP32. And
that's it. The LED with the resistor and the push button with the
resistor is the same as in the level before, in this example before.
So now I have here 2 sketches. And as we had before, I just want to
go through the transmitter very fastly. I have a software serial with
Eric's and TX PIN. I created a software serial, The button is on the
gp04. We have an input.
We're starting the ESP, the softer serum. And here's the logic for the
debounce as we did before, and we are sending here our wire URLs
the string button. But now to the more interesting part, And the more
interesting part is the ESP side. So we're using heat hardware
serum. This is nothing new for us.
We already did this on the channel tool. And now we already ah, let
me see. We have and we should use here also, unconced bytes for
the LED. And we have to use it in pin mode. The LED is in output.
Otherwise, it won't work. And also this kind of, checking if the serial
is available, we already did before. So there we have to do nothing
more. But now in the loop, the interesting part is how we can get the
signal via UART. And we could say, for example, while, and in this
while, but curly brackets.
In this while, we're seeing, serial. esp32 dot available, greater than
0. Then we will be getting actual information here. And then we could
say, for example, string. We see the data is equal to, oop, that was
consuming equals to, serial ESP32 dot read string.
I think this would be the proper function. And now we will get all of
the information here in this string because we are sending a string.
And I would say I would like to print out at the first step the received
data, and we'll take a closer look if this will be our desired data. We
also could do just for training purposes and that we are seeing what
we are getting here. and wind, 8.
It's also in bite, but you can see both of them. bite from Syria equals
serialesp32 dot read. So we're getting the raw bytes in this case. and
it's an int8. Then, we can print out those bytes.
This will be the first step. Then upload the code here. And here is my
setting with the audio in Uno and the push button. Here is the ESP32
with the logic level shifter as we talked before. And now I would like
to click here on the button and in the serial monitor.
I'm starting the ceremony to write again that we see here something
happened. And here is the raw data from our serial monitor. Now, we
are changing back. I would like to have this string then upload the
code. There we go.
Once again, button click, and we're getting the button. Nicely. Oh,
but we got here in one line break. So, therefore, I would suggest that
we, at the Receive data. We could say receive data dot trim.
That will delete all of the white spaces and line breaks. And then we
could say if received data equals the string, what we want to to
check. In our case, it's the button that we are sending out with the
audio. If this is so, Then, we could switch and turn on the LED. And,
therefore, we are toggling the LED.
And we could say, digital write, LED. And now we make a little trick.
and we are saying Digital Read LED. So we are not finished yet, but
I would like to explain what I'm doing here. Normally, I would write
here in high or low.
But now, I would like to read the current state of the LED. If I do that
now, I will get here in law or enforce because it's not on. So if it's
turned off, I get back here and false, and I make a knot here an So I
convert it, I make a knot in front of it. So if this statement is false,
Then I convert it to true with this knot. If it's turned on, then I'm
getting here and true and with this knot, I make a false out of it.
So we always invert the current state and therefore, we are using it
on one liner and toggle the LED. And that's what we have to want to
do. And then I could add a serial print here so that we see that
something happens to toggle LED. Let's see if our function works.
So, it's uploaded.
Then one click. And it's turned on. Next click? Turned off. Next click?
Yes. So we are sending now through a logic level shifter from the
UNO to the ESP32 via UART and string, which will trigger some
LEDs here. And of course, you're not communicating often with 2
MCUs, but you can use this, especially this available when you are
receiving data from a module, and this is exactly what we are doing
also later on. And therefore, it's a nice use case that you can see
what you can do with a logic level shifter with serial available. and
also how you can transmit data via your art.
FINDING OR
CALCULATING THE
BAUD RATE
In this project, I would like to talk with you about the common vault
rates. So imagine you have the following setting. You have here an
existing circuit, which has some external components and you want
to capture the data. But you really don't get any valuable data out of
it, what you can read and interpret. So, therefore, we capture the
data and then we measure the time frame from one logic level and
we can calculate the bot rate.
And this is often used because maybe we can't find any data sheets.
So we are trying to reverse engineer it. And the first step is of
course, we can make an arrow with some common portraits listed on
Wikipedia. We already know the 9600 115200. And also 576 alone
are really common in the audio environment.
So What we then do is we don't know the portrait, but we want to
have it. And the hertz is equal to a portrait. And it's in microseconds
when we make the calculation 1 divided by Hertz. So, we have 1
divided to 57,000 is 17.36 microseconds. So let's do this with our
sketch.
So a lot of bytes should we get back? And this is our sensor and
pm2.5 sensor. And in the registrar between there, we could then
calculate our value, our air quality value. And now the question could
be, how could we check if we are right? This is a good question, but I
have a second device here.
And this second device is just a few centimeters behind this camera.
This is in my office. Here are some more values which were
transmitted to Grafana. So for example, I have here humidity, I have
here something which indicates my light. pressure and here, are the
air quality.
So I have been here in Raleigh from 12, 12 is moderate and could
be better. And here's my temperature. I'm melting, but, with all of the
light, etcetera, and equipment, the room got here really, really hot.
So here, this quality, this air quality level should be read out. This is
our desired state.
It's 9600. Data bits, no parity bits, stop bits, The order is not really
mentioned, so we'll leave it on default. And now it should be the
takes because we transceiver. Let's test it out if we get something.
But before, we have to also power up the PCB with an USB c cable
because the internal MCU should read out all of the stuff here.
So as we can see here, it's starting up. And if it's finished with
starting with studying, then I need a 20 seconds time frame, and
then we get some data. So I clicked on start measurement, and we
can see we're getting here the first data 1, 2, 3, 4, 5, 6. I think it
should be 7. And now there should be an 8, 1, 2, 3, 4, getting a lot of
data now.
Now it is finished. Now, it should be 20 seconds, for example, no
data is transmitted and then it starts all over again with 7 or 6 data
frames. This is good to know for the later purpose when we are
using the ESP to get the data out of it. So 1, 2, 3, 4, 5, 6, 7, and now
the next time frame starts. Okay.
I click and stop. Then turning into one of the data frames, which we
got here. And now the interesting part starts because we have here
now all of these bytes should be our response. And what we have
here is now 1, 2, 3, 4, 5 is TF2. So the 61 should be DF 3 because
it's index 0.
So, the 6th one should it be? So 012 4, 5. There is 0 because this is
good that this is 0 because otherwise I have some health issues
here and 6 because this is our df 4. So we have to calculate df 3
multiplied 256 plus the F 4. So 0 is the first value, and this is our
desired second value.
And it's in hex 4, and this should be 15, I think. if you change it to
hex and as you can see, it's 15. And my internal value shows here,
14. As I can see here, 14 is my second device, and we calculated
15. So this gives us, and first clue, how we could approach such a
new device and external device because The MCU will now make all
of the work by communicating with the sensor.
We are just interfering with it by capturing the data. and reuse this
data. But you can also send those commands via you out to the
sensor and get out the response. In the next project, we're gonna try
to make here our own library for the ESP 8266 to get out the equality
values.
SELF-MADE ANALYSIS
WITH ESP32 FOR PM2,5
SENSOR FROM IKEA
So then I would say let's do some programming, and I have already
attached an ESPN to the board. So here, we have our PCB. And I
have attached some jumper cables and some clamps here because
it's easier to use. That means I have one cable ground to ground of
this tiny twin, 8266, and one goes to an D2, just an input and digital
input. We don't use, no more, but with the ability of a wifi, where I
can now do this little ESP inside this device.
could add maybe some BME 280 sense of humidity and also
temperature, and then we have a perfectly IoT device. I also have
attached, on the other side, the logic analyzer so that we can also
control the values that we are trying to get with this ESP. 8266. And
therefore, I've created a fresh new sketch here . And be aware, we
are now using the 8266, but you can do the same approach also with
the ESP32. And one of the major issues, what we are running into is
that we have here a logic level from roundabout 4.5 volts.
And as we did in the previous projects, we know this could damage
our ESPN. So what can we do now? First thing is we could use a
level shifter. You should use a level shifter. My approach is I'm taking
the lazy way and taking the risk, to damage my ESP, but the risk is
worth it because we are not really by five volts, a little bit bizarre
down.
Maybe this is in the tolerance, and I have my device running for
more than a year, and it works really, really well. but it's not so
reliable. So if you want to have a really reliable device, use a level
shifter because otherwise it really can't, could get damaged. So I am
accessing directly, and therefore, we can focus now on some cooling
stuff. And I would like to start with some software series.
It's very cheap, and we only need one data line. There is not really,
library needed. Of course, we use the 1 wire library, but all of the
other aspects we can do very easily on our own. And the next real
advantage is that we can combine the VCC line and the data line So
we only need 2 GPS that means ground and one line for power and
data communication. And this is really cool.
You can go even further. You can use, many of the dealers yeah,
18b20 simultaneously on the same GPU. How does that work? Each
of this temperature sensor has this unique address And later on in
the sketch, we can address the sensor bytes unique address and
then can fetch your temperature data. Yeah.
We need a pull up resistor. That's how this whole communication
works, and it's like, time based communication. And as the last point,
we also get here in CRC, check some that we can, and have a
reliable temperature setting in our MCO. So in summary, this is really
cool or a cool protocol. It's a single bidirectional data line for
communication, to make it simple.
too and caustic cost effective to use. This is the reason why it's really
popular, and I would like to cover these one wire protocols as well
because it's good training for us on how we can access data on a bit
level.
DS18B20 SKETCH AND
WIRING
One of our first steps will be to create a simple sketch so that we can
communicate from MCU to the module. And then we start with the
wiring part. So first of all, we are connecting W5 and the ground to
our breadboards. can also use 3.3 volts. So on the left pin of our DS
18b20 sensor, we're connecting ground.
The middle pin, we connected to some of your GPIOs. You can also
use another Uni or an ESP 866 if you would like. Then we have a 4.7
kilo ohm resistor, and this is our pull up resistor. So we need to put in
a slap. And therefore, afterwards, before we are connecting to the
GPO, afterwards, we are connecting the line to the VCC part so that
we have a resistor and pull up logic.
And on the right pin, we have here our BCC. So this is in a short
summary, our wiring part. I have implemented it now with, tiny
ESP32. This is one from the studio, but you can do it as well with an
d 1, with a dev board. What do you have in your equipment?
So the clamps from the logic analyzer are connected directly to the
center. And that's it. That's all that we need to do here. So let's
switch back to Visual Studio Code And I've prepared here my basic
sketch, nothing special in it. And let's take a closer look at that
platform for him.
So this is just a function call. And so now we are creating some flaws
here. For example, temperature in Celsius because since the
Celsius is the default value, And then we say sensors get
temperature by index. and this is, how we get here the temperature
value. The same thing could be done for the recalculation for
Fahrenheit, and then it's not get temperature c.
It's called Let's see. Get temperature f by index 0. Okeydoke. That
looks good so far. Then we can print out the values.
So I have already prepared the serial print here. And now, it's time
for the first check. Let's see if you have some typos, no, then upload
the code. And there we go. We have the values here.
And as you can see here, it refreshes every 2 seconds. And I am
here in 29 degrees. It's mid June 2023. And now I would like to have
a little bit of compression because I'm really melting here. My
temperature sensor behind me says it's around 20, 32 degrees.
This is because of the light, many monitors, the PC, etcetera, and,
it's not the perfect condition in the summertime to make some
projects here. But back to the 1 wire protocol, as we can see, the
first step we have already accomplished so we can get here the
temperature in scratch, degrees Celsius and also in Fahrenheit.
EXAMPLE DUMMY CODE
Wiring:
void setup() {
// Start serial communication
Serial.begin(9600);
// Initialize the DS18B20 sensor
sensors.begin();
}
void loop() {
// Call sensors.requestTemperatures() to issue a global
temperature request to all devices on the bus
sensors.requestTemperatures();
// Get temperature from the DS18B20 sensor
float temperatureC = sensors.getTempCByIndex(0); // 0
refers to the first DS18B20 on the bus
Let's make here one second Then it makes sense to Leave here a
little bit more time a little bit higher resolution and get the accurate
value as we can see here But this is a matter of what you want to do
with your sensor but I just want to show you how easily you can
change it with this kind of set resolution function and that you know
that the values are Really be rough enough with 9-bit solution
Resolution and it was much more detailed with the 12-bit resolution.
ADDING AN EXTERNAL
DECODER
In this project, I would like to demonstrate to you how we can import
external decoders from third parties in Python. And I found here an
external decoder for the DS 18b20¢. So on. And although, or maybe
it's already implemented in PowerS viewer, Let's use this use case to
show you how we can do that. So there's, guy who wrote this
external library, really Chrome, and we can download this folder.
In this folder, there are 2 Python scripts in, which do all of the magic.
And where should we copy and paste this folder? To use this
decoder, just copy the folder and its contents to user share lip lip
sync rock decode decoders. This is exactly what I have here, but I'm
using a Linux system here. So if you are running a Windows
machine, the first thing is maybe you should consider changing to an
analytics distribution in your private little area because it's free, it's
open source, and it protects your data much more than Windows
does.
And it's, and more the way we are using software in the audio
environment. Just, and show notch. And also, I have a good Linux
Project, also on this platform. So maybe you are interested in this
also. But back to the topic, when you are running a Linux machine,
maybe you have the same folder structure.
It could also be the case that this is a hidden folder. So just try out in
the explorer, typing in user, share, etcetera. And then maybe you find
here in the folder structure, this lip sick rock decode, and then you
can copy the files in it. So let's open one of these first, Python script
to see what is in it. So first of all, we can find here all of these
commands and, what we've seen in the datasheet, scrolling a little
bit down, then read scratch cut.
This is also what we have read before. And this is really cool. You
can see here, the 0.0625 gives us feedback for the resolution.
highest resolution, lowest resolution, we are reading here, the
resolution out. And here is where the converter temperature is buried
now.
And This is exactly the same as what we did before with our
blackboards, but here it is done via Python. Really cool. And now
when we have copied this folder to the structure, then we reopen our
pulse view so that it could be initialized. And then, so restart, pulse
view. And then we can search the decoder here.
by typing in DSO18, double click on it, and then we can change the
one here, for example, and we should see something here. So let's
see here. Let's go a little bit out. And we see here our data, the
temperature into 30.375 as we had it before. in our audio, you know,
IDE.
It will be in the last byte because you also see here the resolution in
12 bits, etcetera, really, really cool what is made. But we did before
the approach by reading it ourselves, but as you can see here, with
this externally It's much more convenient. And maybe for your
purpose, you need another external decoder, search for it or even
write your own decoder with the past few. This is no problem.
GET DATA ONLY WITH
ONEWIRE LIBRARY
In this project, we are just using the 1 wire library to get the
temperature data. And therefore, here and finished example
because it's a little bit more of programming work. And I go through
the main concept, and we also do an analysis with the logic liver to
show you how we could do that. So here is the finished example we
are just needing here: The one wire in Dallas we could get rid of. So
let's compile it in the meantime.
And we are creating, including the one wire because this is our main
communication to the whole module. And what we are needing here,
let's go through setup. We have here an and function which is called
Dellus. And with this Dellus, we just initialize it and then we are
fetching the data. So let's see what this tells us.
We are just, pass here 1, that means is it in start or not in the start.
So let's see. We're making a 1 wire option here. We are getting an
array with 2 spots and an integer with result and also in float
temperature. Then we are set, we are resetting the object which
indicates that we are starting to communicate. And then we are
sending the first command and it's the cc command.
So then we are combining both of the values. And let's see CCH.
What is this register that skips RAM? The master can use this
command to address all devices on the bus simultaneously. without
sending any rom code information. For example, a master can make
all DS on the bus perform simultaneous temperature conversion by
using a skip wrong command.
This is what we do here. We're just addressing all of the temperature
sensors, what are here. Then note that the read scratch bat
command can follow the skip RAM only if there's a single slave
device on the In this case, time is saved by allowing the master to
read from the slave. Okay. In this case, we are using the BE room,
the next one.
Okay. Cool thing. That's all we need to perform those actions. And
this is what we do here. So no, not starting.
a 3 is the pin, the GPIO, where the sensor is on, and that's it. So I've
already uploaded it. So let's see what the serial monitor prints us out.
Now we can open up the pulse view again, and let's inspect what we
get here. We should now get here and a shorter time data frame
because we are just sending here.
Let's see a few commands out, and that's it. So we click on stop,
zooming in to the time frame or to the date frame. And we can see
here we have here our skip RAM, our BEA command, and then we
have these two bytes where our data from the sensor is stored. Then
we're reset. We're sending CCM and, next reading command out to
the sensor.
That's it. That's all that we need. And this code can be fulfilled also.
For example, with an 80 tiny with limited space, etcetera, we just
need an and 1 wire library. And there are also some tiny 1 wire
libraries so that we don't have to do all the things by ourselves.
and not really more is needed for getting temperature values with an
arduino or ESP.
CIRCUIT AND WIRING
And as always, in a new chapter, we start with some basic sketch
and the wiring part so that we then lay down and can't catch the
data. The DHT 11 or DHT 22 is also communicating via such icon 1
wire protocol. It's not really standardized, but, therefore, we're only
needing also here one data line. So VCC and ground is to Bradbot,
you can choose 5 fold or 3 point three volt, and then choose 1 GPI at
home for the data communication with your USB 32, for example,
per. I'm using here, as you can see here, on prep, so, a suction
module, and this module has already and re system, pull up our
system at the data line.
If you are using the raw temperature sensor. Then you need, I think,
10 kilo ohms, 4.7 kilo ohms resistor and pull up resistor to get here
the data line. high. Also, I fear the wiring for both of the sensors, but
I'm only using the DHT11. It's the cheaper version, but not so
accurate.
But for the most part, it's absolutely sufficient enough. But if you
would like to have more range here because I think that DHT 11 is
can't or is not able to be measured here, a minus degrees. So,
therefore, you need a DHT 22. much more, range of the temperature
and humidity and also, more accurate, but costs a little bit more. The
approach is nearly the same as we see later on.
you have to calculate it a little bit differently, but you can follow me
along, but have to adapt certain steps then later also in the
calculation. Yeah. That's it. That's the first step of the rhyme part,
and I also connected here the clamps for the logic analyzer directly
to the pins on the GPIO, and that's it. All that we need.
Nothing more to do here. And then we are switching to our basic
sketch. And what we are doing here is, we are adding here either
food sensor and the DHT library. That's the first thing we need to do.
And it is under Arlino IDE, then you can just search for the DHT
Center Library for Adafruit, and both of them can be used for the 22
and the 11.
So then let's start by defining, yeah, the pin. And I think the example
does it with some defines and not with const bytes. So let's keep it
like in the example from the library. define the HD type, and this is in
my case, DHT 11. If you use 22, then you have to change the type
here.
would use here defined HD type DHT 22. Then I'm using here an
unsigned long previous Millies. That's correct. And I'm creating an
object here. A DHT object, write this, with the name DHT in some
lower letters.
and I have to pass here the DHT pin, which we defined and the DHT
type. So it's much more readable if we're using the definitions. Okay.
Then dht dot Begin And now we can start in our loop with getting the
data. And therefore, we are using some floats here for humidity, DHT
read immunity, I think IntelliSense is such a good thing, or you just
start typing and getting the name of the function.
And this will be heated . So just that, you know, that you can also
calculate it. And we take a closer look also at this computing heat
index function later on when we're inspecting the library. So now we
are printing out all of the stuff. I've already prepared the server
printout, and then we're uploading the code and opening the serial
monitor, and we're getting 30 degrees straight and, humidity 57, 58
heat index, 25. Okay.
Sounds good to me. For the first tests that we get here, the humidity
and temperature data, that we can proceed.
UNDERSTANDING THE
PROTOCOL AND SIGNAL
ANALYSIS
Now let us start by inspecting the protocol a little bit more in detail
from the DHT 22 or DHT 11. So I have both data sheets here, but we
are focusing only on a DHT 11. You can follow me along with the
DHT 22, but have to adapt here. on, details. So, therefore, let's close
this one.
Open up the DHT 11 data sheet. scrolling a little bit down so that we
can see our first in force what we are needing. The first thing is that
the temperature and humidity accuracy is not really in a way that we
could say is an industry standard. But in our spare time hobby
project, thickering projects, it would be accurate enough. But you can
see M 2 degrees is some significant change and it could make a
significant difference.
Scrolling a little bit down. We can see here our resolution has 8 bits.
It's also not really accurate, in comparison with the DS ATB 20, for
example, also the DHT 22. typically application with this, pull up
resistor. And also you can see here, if you have longer distances,
you can reduce the pull up resistor, for example.
Here, power and pins are also interesting. We can use here the logic
level from the ESP32, the 3.3 volt, and also the 5 volt Ardino, both of
them are sufficiently enough. And you could add here a capacitor for
filtering out any noises. So here, the communication process, this is
what I would like to see here. Singapore, why are two ways
interesting?
So it's just not a standard format. It's a single bus data form from
RTCs to communicate and synchronize between MCU and DHT
sensor. It is about 4 milliseconds, the whole process. And the data
consists of 40 bits. Okay.
Interesting. and start with the higher data bit. Okay. That means we
can read from left to right. Here the first eight bits are the relative
formality data, the decimal data, then we have the temperature and
the decimal temperature.
And then check some If the data is trans transmission is right, the
checksum should be the last 8 bits. Okay. The sum of this I see. I
see. And here we have a good overview of the communication
process.
And this is now exactly what I would like to see. We can find out the
same thing, with our example here. So I've, my sketch running. You
can see a certain degree Celsius. And I have here, the humidity by
58. The PulseViewer is already set up.
1 channel, no decoder. I have added 1 gm here and 8 Megahertz
could change it also for 60 megahertz, for example, and let's run it.
And now it would be interesting if we cut the communication because
then we can be sure that we have the right values here. So Stop.
Here we have.
Here we have the last value. So the last value should be 30 57, not
58. And this is what I'm now would like to go a little bit more in detail
with the communication process. So we're focusing and zooming in,
then let's open the communication process here. Let's go a little bit
down.
So let's have here the senior bus free status is high voltage level.
This is all before what we have here. You have the high line. And
then when the communication between the MCU and DHT 11
begins, the program sets the data voltage from high to low. This is
this area.
And this process must take at least 80 milliseconds. So let's
measure. What do we have here? bum. bum.
Okeydoke. Then we have 20 milliseconds here. And this is because
the light Bree is set here at 20 milliseconds, and we will see this in
one of the next projects when we also investigate the library. So this
is exactly what we want. 20 milli at least 80 milliseconds to low.
And then the MCU pulls up the voltage for 20 microseconds. So
we're zooming in. We have here the next high, and this should be 20
mil microseconds, not seconds and with your 13 micros 13. So I
don't know why we are not following the standards here. It could be
that this is some intentional purpose inside the library.
I don't know, but it works. So here, we have a little glitch. the DHT
sends out a response signal and keeps it for 80 microseconds. So
this is now, if both can communicate with each other, and we have
here 80 3 microseconds. And then, DHT pulls up voltage and keeps
it for 80 microseconds.
And this is where we have started with intolerance. So we have here,
the rest of the 40 bits should be here. So let's go down a little bit.
Here's the explanation in text form. And now, something really
interesting in this kind of protocol is that we have between each data
and spare time from around 50 microseconds, as we can see here,
or, for example, here, these are the 50 microseconds.
And now, how can we interpret the data? We have here some short
impulses and some longer pulses. So the short pulses which run
about 26 to 28 microseconds, are logically 0. So here, we have 23.
Here, we have 23.
so this focuses on the Data length, is, yeah, 28 microseconds and it
is in 0. And if we have 17, 17 microseconds, then we got here and
logical 1. And this is now how we can calculate our values. So going
up a little bit because we want to have here first one is the relative
humidity and then the decimal values. So let's focus on the first eight
bits.
1, 2, 3, 4 of 5, 6, 7, 8. Open up the calculator. Change to binary, and
we're typing in from left to right is 00, and this one is three times a 1,
then we have 1. 1. Hit enter.
And then we are changing to decimal. And we got here at 57. 57 is
the last value we got here, humidity, because 58 was the previous
value. Amazing. It works.
So jumping back to our calculator. The next eight bits are the comma
value. So 1, 2, 3, 4, 5. 678. As we can see, 0, we have 8 bit
resolution.
So not really anything here. To mention, then the temperature data
should start. 345678. changing to binary, typing in the binary data, 3
times in serum, 4 times 1, and 0. Hitting enter, we can see here the
30 in the decimal values and this is exactly what we have here.
Then we have 1, 2, 3, 4, 5, 6, 7, 8. 1, 2, 3, 4, 5, 6, 7, 8. And this is,
the comma values from the temperature. And now the last 1, 2, 3, 4,
5, 6, 7, 8 is also interesting because this should be the checksum
and the checksum should be the sum of all of the values. So that
means 30+57 should be 87.
If I'm calculating your rights, changing to binary, we have and 1.
Now, 0, 10, 10, and 3 times the one hitting enter. And we see 87 is
the desired value for our checksum. And now we can be sure that
we have read the values in the correct way. The last spare time and
then the data line is set to high And this means the communication
process is over.
Very good. And this is how the DHT 11 sensor communicates with
an MCU, for example, so let's see how long the whole data process
is. It's only 20 milliseconds? No. It's 20.4.
Ah, not really 24 milliseconds and what was in the datasheet around
about 4 milliseconds. Yes. 4 milliseconds is the whole data
processing. Yes. 4 milliseconds.
That works really well. So we have now checked everything on a bit
level, on a database level, and In the next projects, we're trying to
find out if we have it here also in the decoder.
USING THE DHT11
DECODER
And of course, there are also some decoders out there. But to
understand the protocol and all of the necessary content of the
datasheet, it's necessary that we are not jumping too fast into some
decoders. Also, it's beneficial and helps us, supports us. But at the
first point, just try to understand all of the meanings from a
datasheet. Then we open up here and we are typing in, it's an
AM230, because this is the sensor inside this DHT. We click here on
Enter.
Speaker 1 (00:00:38) - And now we can add here the data line, this
is D0. And now here we can change between the DHT11 and the
DHT22. And if I'm choosing here the DHT11, now we can get here in
a convenient way all of this stuff that we did before by counting the
binary values. So as you can see, easy doing, easy as an overview,
but not really helping us to understand all of the content. But why not
use it when you are trying to find out if there is some error, for
example, in your circuit.
INSIGHT INTO THE DHT
LIBRARY
And if you are more interested in how the library is working, you can
inspect it by clicking with CTRL and, for example, a mouse click. You
can jump into the DHT folder, you can also use the Arduino IDE, you
jump into the folder structure where all of your libraries are saved.
And then you can always find a CPP and .h because this is in an
object-oriented way. That means here in the header data is
mentioned all functions and variables which are used. And in the
CPP you find all of the code from this library.
And you can go through this library to understand how they
managed to read out those values in Arduino code. And I would like
to go a little bit down so that you can see, for example, that the DHT
library implemented the pin mode so that we can read the values.
Because often we have to do it on our own, but here, when we are
starting in the setup to begin, this will be done here. Then going a
little bit out, reading the temperature.
Then we can see, for example, that the sensor is now read out with
some helper function, converting the Celsius to Fahrenheit, for
example. I'm going a little bit down because I have the DHT11 here,
this will be something for us. But it's not really interesting here. I
would like to see if we find a little bit more here, compute the heat
index. Also interesting how this works, you see a lot of mathematics
is going on here. Pow wow wow, much rocket science. Here, read.
This is what was the interesting part.
Because we have the 40 bits to receive all of the data. So we have
an array here. And now the magic begins with, for example, let me
see, DHT11, delay 20. Datasheet says at least 80 milliseconds, 20 to
be safe. This was the first start. So this line, no, here we go. This 20
milliseconds is done here with this delay. And then some magical
things will happen where the start conditions start. So that means
this was this part with the, what was it, 80 seconds down and high.
And then the real magic starts with interpreting all of the zeros and
ones by measuring how long are the pulses. So it's not really easy to
measure here without any interruptions to the pulses. But you can
inspect it if you want to go a little bit deeper in this whole thing, how
this library will do it. So they make a change if it's short or not short.
And then, yeah, they will calculate the temperature and the humidity.
So you see, simple to understand, not so simple on the Arduino code
basis.
But we will do our own libraries later on with the E2C. It's a more
common way. It's a more, I would say, convenient approach to write
your own libraries. But here you can investigate how it's done for the
DHT11.
BASICS
Now, we come to, next protocol, which is called SPI serial parallel
interface. And this SPI is also widely used in the audio environment.
So if you're using an AT tiny, RT Mega, and ESP, you will find
general GPIOs. The thing is, if you are using a microcontroller, which
does not have so many in GPIOs, SPI wouldn't be the 1st protocol
with what you are using because we are needing 4 data lines, 1
ground. So 5 lines, jumper cables, 5 GPS.
And this is not often very suitable for all circumstances. Also, we are
not there's no need for a full duplex communication. And I come to
this later on because in one of the next chapters, we are talking
about the e squared c, which is widely used and only needs tools.
lines, jumper, cables, connections. And, e squared, he can also only
talk half duplex, and here we can talk in full duplex.
So that means we can transceive and transmit and receive data at
the same time simultaneously. And this is used for example, ink
displays and other modules were NFC, for example, RFIDM. And Of
course, it's a convenient way with this full duplex, but we have to
think about, is it really necessary? Because I also have RFID and
NFC modules where I use your art, for example. And this is much
more convenient when it comes to tiny microprocessors.
So therefore, it's used, but I'm not really a fan of. So this is also the
reason why we keep this SPI a little bit shorter in the dynamic. So
let's focus here on what the SPI have, on what the GPOs are. First
of all, we have a clock SCL. And this is a clock input to synchronize
data.
transmission for master device. Same as in the e squared team. So
that means if the pulse goes high, then we send the data. going low.
This is the space.
And with the next pulse, we can send the next data, and this is how
we can set the clock into nanoseconds, for example, and can
transmit data very fastly. The mostly stands for master out and slave
in. So we are transmitting data. and the meansu stands for master in
slave out for receiving data. And this is because it's in a separate
line, full duplex.
Then we have slaves selected to activate and deactivate certain
units and communicate only with a certain number of units because
we also can use this as some parallel bus system. So that means I
can use several slave devices here for 1, master. And I have to
change the SS or CS pin here. Of course, if I have 2 slaves, I have
to use 2 different SS or CS pins. So this is a variable pin often, but
the clock meso and mostly are predefined in the outer, you know, or
ESP, for example.
Yes. And this is how on such a parallel interface you could set up. As
you can see here, we have mostly mesone here. connected in a
parallel way, and then we have 3 in the master different slaves
selected. So then I can say, I would like now to communicate with
slave 1 when I'm putting high, for example, to SS 1. And as you can
see in parallel devices, many different MOSIS, SPI devices can talk
to 1 master. Yes. It is and where it's a I would say it's a the SBI has
his justification it's widely used, but as you can see, you need a lot of
wiring, therefore, So next project, we take a closer look on what we
can do with the SBI.
SKETCH ARDUINO UNO
TO UNO
In this project now, we want to make our own SPI environment. And
therefore, I have FEM 21 here. Audio clones which should
communicate to each other with our over SPI. And the connection is
really simple here. So we are using the predefined GPS, what you
can find here on your reference, your pinout from the manufacturer,
And normally, it should be, on the pin GPS open 13, the clock pin, 12
should be a meso on.
11 should be mostly. And 10 probably you could set up the SS pin for
example. but there's always checking your pinout. I've mentioned
here in the pins also in the codes. So what I did is I connected all of
them together, that means 10 to 10, 11 to 11, 12 to 12, and 13 to 13.
And that's it for our first catch. And afterwards, we also add, here,
some sensors, but now we just want to transmit here one data that
we can see if the communication process is working in the right way.
So I have here the master on the left side and the slave on the right
side. So that means the master sensing command, and we want to
get your data from the slave, for example. And therefore, I would say
that we start with the master.
And the first thing, what we are doing here is we are setting the SS
pin. So we say const integer, for example, SSpin is on the pin 10
because this could be variable. And then we have to include. This is
also one thing. in the standard repertoire in the core from the Ardeno
is the SPI library already implemented also in the USB environment
So, therefore, just include it.
And then we have access here. For example, to sbi. Begin. Then in
the next line in the master, we are setting here some speed
variables. That means we can say here, SPI set clock divider, and
you can search about this term.
You find different kinds of explanations here. What we are doing
here, we are setting the clock so you can see a different kind of
dividers by 8. This will be probably the best speed setting for our
purpose now. Then we are setting, the digi a digital write, and we are
setting up the SS pin. too high because this is the normal default
value.
And when we want to start the communication, We have to set it to
low and then make the communication process, and then we are
setting it again too high. This is also the first step of what we are
doing now in the loop. So that means inside those 2 digital rights, we
have to make our communication process. So we are setting
ourselves low. And afterwards to hire, this is where the
communication can start.
So what we are doing now, we want to send, for example, one to buy
it. And despite being able to have the following information, a byte
data to send, for example, and we are sending here, yeah, let's send
here, for example, this value. Then spedot transfer and we are
transferring the bytes, which we just initialized. And then we could
say digital, digital. Alright.
Because I also want to see what we are printing out. SSIN. It's
digital. Yes. That's what we want to see here, the data to send.
in hex, and I also would like to add here and serial print, not done.
Print line, and here we say serial print. We could say there were no
leaks. So it just looks very promising, then we try to upload this code.
Very good.
And our first test was successful. That means we are sending out
every 2 seconds here this value, but no one can really get it so our
slave can't be able to receive it because we have to program it.
Therefore, jumping to our slave, we have to do nearly the same.
That means we have to define or include the SBI, and we have to
define here their SPIN as well. Then we are starting with the SPI.
And we are making a pinmode here because, here, we haven't really
defined it any. So that means it's an input pull up. We don't need this
in the master, and now we would like to configure the SBI settings. in
the slave. So, therefore, we say SPI set. Begin transaction.
And now we have to fill in our SPI. I think it's called settings. And
inside this function, we have to hand over some values here. First of
all, I would like to have 8 Megahertz here, and this should be in
accordance with where the SP clock divider is? Then we are seeing
the MS B should be transmitted first, and I have E and SPI mode.
And this is also where we have different kinds of modes where, in
this mode, the slave selection should be lower when we are
communicating. This could be changed by SBE mode 1. I think then
it's, we're communicating by high, otherwise, by low. So we can
change the direction here as Okay. And that means in the loop, we
say if, for example, the digit term, read of the SS pin, and this is the
reason why we set the pin mode.
If this is low, then the communication could start. And then we say,
for example, we are receiving here the data from the master. So
byte, receive data equals to, spe.tran sphere 0 because we are
sending here back Justin 0, and it's in full duplex. That means we
can receive something here and send back data. But I would like
only to receive the data here.
Nothing sent back. But otherwise, we could also see here, for
example, here, we could receive the data as well. So then, we want
to print out the data, which we have just received. So received 0x,
and we're saying 0 print line, receive data in hex. I have already
changed the USB settings here.
So this should be here on USB 0, and this one should be run on ESP
1. Then let's see if we have some first connection here. Then let's
see. Open up the terminal again. And there we go.
We're already getting some data here. Okay. It Looks like it works
perfectly. So that means we are now able to send here data in bytes
level via SPE from one device to the other device. And we also can
send back some information here as well.
We just have to grab it here. and catch it, in this line, for example,
and then we have here in full duplex. Way. One of the next projects
we're analyzing this data communication on the logic analyzer. And
then we also want to send you some real life data, for example, from
a temperature sensor.
ANALYSIS WITH LOGIC
ANALYZER
And now let's focus on the PulseView part so that we can
understand the SPI protocol a little bit more in detail. Therefore I've
already inserted the clamps on the Arduino UNO clone. So I've just
pushed up the jumper wire a little bit so that I get the clamps inside
and also that the data communication is working reliably. And now
I've already started communicating here. I've added 10 GHz here
and also in the sample speed I've changed to 24 MHz. And this is
what we got here on these four channels.
Now let's focus here on the data frame that we captured here. And
it's pretty obvious what kind of channel we have here in our SPI
communication. So the D0 channel has obviously the clock values
here. So you can see here the same width for low and high. So let's
focus here and measure what is the time frame from one of these
values. And we have here 250 ns which is equivalent to 4 MHz. And
this is pretty fast what we can do here and transmit and receive data.
On our D1 channel we now have the data that we are transmitted.
The D2 channel is the receiving line and this is some serial noise
that I captured here because we do not really receive any data here.
So I'm not really evaluating this channel. The D3 channel is our chip
select. This is what we are set to low in our Arduino sketch so that
we know what kind of module we are communicating with. And now
let's focus on this one. This is our transmitted data. And when we are
focusing in here we can see our signal is starting on a falling edge.
But this is not really interesting for us.
Interesting is the rising edge of the clock and this is where we can
say here we have a logical 1. Here we have the next rising edge and
here we have a logical 0. And then we have here a logical 1 and a
logical 1. So in summary we have now when we are changed to
binary 1011. This will be in decimal the number 11 or in hex what we
send B. And this is exactly what we did before in our Arduino sketch.
So this is just the poor raw data but we can also use some external
decoders here.
So just click here on the decoder and then we are searching SPI.
Then we are adding the SPI decoder by double clicking on it. Click
on the settings and here we are referencing the different kinds of
channels. We have the clock on D0. D1 is our transmitted data that
means this is master in and slave out. This is not what we want
because here we are still receiving data lines. We have the master
out and slave in. This is the D1. And then we have the chip selected.
This is the D3 line.
And this is now what we have before made on our own with
evaluating all of these values. And as you can see here we have the
bits and the data that we transfer with the decoder. So a simple and
effective protocol. As you can see very very fast and we have a full
duplex here. That means we can send and receive and transmit data
at the same time simultaneously.
DHT22 VALUES VIA SPI
Now one of our last examples will be that we want to send real life
data from a DHT22 for example via SPI. So the wiring part is very
easy in this case we are just connecting the DHT22 to for example
3.3 volts ground to ground and the output goes to 5 to the GPIO5 for
example and the connection what we had before keeps the same 30
to 13, 12 to 12, 11 to 11 and the chip select is on 10. So of course
you can also use another sensor and if you're using not the breakout
board don't forget to use here a pull-up resistor for the data line.
Okay so our existing sketch was here. Here on the left side we have
the slave and I've already had the sensor. So what we are doing now
is we want to adapt the first sketch from the master. So make it a
little bit bigger here. Closing the serial monitors for now. So let's see
this is just everything the same but what I would like to do here is we
have to add the library for the DHT sensor. So the first step will be
adding the DHT sensor library and now we could add here all the
things that we need.
This is the most significant, if we could say so, the higher byte. And
then we have here the lower byte, because here we are getting back
both of them in one single connection as we have here. So therefore
one wire read, second wire read. And here we have the first one in
and here the second one. So let's test this. I would say I would like to
see if this is already working.
And let's print out the buffer one because I have not so much, so
much light in here. And I would like to have this in binary. Then let's
print it out. So we are already getting some data out of it. That looks
promising to me. Let's see if we can check if this is right. Zooming a
little bit out, stop. Zooming in. And this should be one, one, one, two,
three, four times and zero and one. Yes, okay. Looking good to me.
That means we have our first communication established just with
these few lines of code. Isn't that amazing? I think so.
So then the next step is the calculation because we are not finished
yet. We are implementing the high byte, the low byte divided by 1.2.
So now we need our basics from the bit operation and also the bit
shifting. So we are creating an integer. You can create a long as
well. Then what we do now is we are saying we want to have here
the buff from index zero. And now try to find your own solution and
then click on pause and then you can proceed. So it will help you
much, much more in the learning process.
So what we are doing now is the first high byte will be shifted eight to
the left. And then we are making an operation here, an OR operation
with the buffer one. And all of these will then be divided by one.
by 1.2. So, let's see if this is working and then I will give a more
detailed explanation. Just check if this will work. Okay, we are getting
85 lux here, looking good to me. Then I will start the flashlight here
so that I'm seeing a little bit more.
As you can see here with the flashlight I also start the evaluation with
the logic analyzer because here is the clamp exactly before. So, then
I would like to stop here as well so that I can show you what I was
doing here.
Zooming a little bit in and we're getting out one of these values
should be stored here. So, once again open the blackboard and
what we did now is we're getting the first in the index 0 we're getting
those data and this is the... 4 5 zeros then we have a 1 0 1 and this
will be shifted to the right. So, what we now have is 1 0 1 because
the zeros are not interesting for us then we have 1 2 3 4 5 6 7 and 8.
Then the logical OR with the buff 1 means that we are adding here
on this space the low byte.
So, that means we have here 0 1 0 and this 1 2 3 4 and we are
adding here the 1 1 0 and 1 1 1 1. So, our new value will be this one.
So, in each place where we have a 1 we also have here the 1. Here
we have a 0 and then we have 1 1 1 1 and if I don't mix up anything
here this should be our new value. So, let's type in in the calculator
or change to binary then we have 1 0 1 1 1 1 0 1 1 1 1. So, this
should be 1519 1519 divided by 1.2 should be and decimal divided
by 1.2 is 1265.83. Let's see if we have that. We have 65.83 and here
we have 1265.
So, we are transmitting here 0x10 and then we are saying via end
transmission we're making a short delay I think this is also written
there that we have to make here and pause between where was it
here wait to complete the first measurement roundabout maximum
180 milliseconds this means maximum does this measurement
means so let's do this one then re-upload it again and there we go
we don't really see an exchange and change but let's see here in the
logic analyzer we can see that we sent here the resolution making
200 milliseconds pause and then we're getting the data and this is
how we could also send here to and register some commands
changing something and then getting data in the read modus and
also here was just an easy line and we will see in the next chapter
with this dht 20 for example that it's also easier but a little bit more
tricky to evaluate but this will capture in the next chapter.
SKETCH AND WIRING
DHT20
Let's examine the next sensor, and this will be the DHT 20 and
temperature sensor similar to the T2, but here with E squared C. You
can identify it with a black housing most of the time. And I already
had it here. So also my connection points to the e squared t pass are
I have to I haven't changed because, they are all connected
together. So, therefore, we open up a fresh sketch.
And what we are doing now is we are using the DF Robert Library
for the DHT 20. Then let's start. Let's start by including the library. So
our main goal will be that we have a simple sketch, which gives us
the temperature and humidity so that we then later on capture in
pulse fuel all of the data. And then setting up this use case, writing
our own library or our own sketch for getting the data.
Okay. That's it. Then we are creating the object it's called dfrobot, I
think, DHT 20 equals to DHT 20. This is the name of the object.
Then we start it, dht20.
Begin Not Rocket Science. And as you already know, in the loop, we
are using 2 floats, I would say, float DHT 20 temperature,
temperature equals to DHT 20, and call it temperature. Easy as it is.
Then we have DHT 20, humidity equals to DHT 20, get humidity,
from measurement range 0 to 100%. So I think I'm not really sure
that we have to multiply it by 100 so that we get really exact values
out of it.
So let's test it out. And then we are printing out both of the values
and uploading the code again. And there we go. We have nearly 29
degrees Celsius inside my office and 47% humidity. Okay.
These are the values, what we are getting currently out of this
sensor. So we have set up the first step to evaluate the Sequencing
communication now.
ANALYSIS DHT20
Ready for some analyzing. Let's start by analyzing the DHT 20 data
sheets and then we are focusing on pulse fields so that we calculate
our values. So here you can see it's an upgraded product for the
DHT 11, and it has a little bit more accuracy than the DHT11 and
also the voltage support is really good because we can use here an
ESP or an Alrino owner, for example, So let's go down to business
and also to page 1010 because here we have more about our
communication process. So here we can see the address. It's the 0
X38 that just some e squared c standards, blah blah, what we and
what I would like to focus is those two parts.
with the register AC3300. So then let's see. Can we find those
values here? Yes. Sense of reading process.
So after power on, when no less than 100 milliseconds before
reading the temperature, blah blah blah. Wait 10 milliseconds to
send the AC command is the trigger remeasurement. This is what
we do here. And this command parameter has 2 bytes. The first by
this, 33 and 0.
That is our trigger command. And this is also what we have here, the
AC and data. Perfect. And then wait 80 meg, milliseconds for the
measurement to be complete. And if the restart does 7 means it is
completed.
So 80 milliseconds. Let's check if we can also see if this is correct.
What do we have here? 20 milliseconds. No.
really the 80 milliseconds. What do we have here? Roundabout, 20
milliseconds. And then the next process started. So let's see what
we have here.
This is not really according to the datasheet. I don't know why it's not
on the initial purpose, but nevertheless, it will function because it's
the DF Robert library. So then we get the next few inputs. We have
here the e squared c address as well, of course, then and
acknowledged. And now we have a read bit.
and we got here. Well, let's see here. so now. No. No.
1, 2, 3456. 123456. Okay. The CRC is missing then because here
we have the state And then we have 1, 2a half. And this is a little bit
trickier afterwards to get the data out of it.
1, 2a half. and 2a half for the temperature. Okay. So this is the signal
flow, and here is a calculation of what we need, then let's start with
some analyzing process. So this is the state byte.
I don't care what the state means, but I care the next two bytes are
humidity and the first nipple for pits is also immunity. So let's check
that those values should be the humidity value. We're starting here
on the left side by typing in the first byte. 0. So first, I have to change
to buy a rim, then 011110000 is the first byte.
Second bytes 00101011, and the first four bits here are 1 10. Enter
changing to decimal value. And now I have to end here. The relative
humidity can be calculated by dividing by 2 to the power of 20. Enter
multiplies 100 should be something like this.
Let's see what our last value was. 46.94 Wow. Cool. It works. Okay.
Let's do the same thing for the temperature. So that means we have
here 2a half. Let's go a little bit out and zoom in, that means this is
the first half, pick the first liberum, and then the last 2. Starting here
with 0110, and then despite, or we have to change to binary. Once
again, then starting with the sec, the first full pipes, this one, 011s
00100.
Last byte, 10010110. And done, Digital, then we have the
temperature divided by 2 to the power of twin no, 20. Multiply 200-50
is 29 Degrees Celsius point 91. Not really. Here we have a little bit
of, it's not in-depth detail, but it's, some comma.
Let me see. But we can lift with this accuracy. And, therefore, I would
say we have already mentioned here, yeah, could comprehend the
data flow from the DHT 20 sensor, and wouldn't it be nice in the next
project on we write our own library with this kind of knowledge so
that we can get rid of the DF robot library
SKETCH DHT20
WITHOUT EXTERNAL
LIBRARY
ready for the next coding challenge. So now, we want to have this
flow inside our arduino sketch. And we go step by step and also how
we can evaluate and also we are investigating then afterwards our
own library with the pulse view. So first step, I would say, let's get rid
of all of the things that we did before because we don't need any
extra library. So also the library here, we can pick out then we
include here the wire because this is our helper library.
And then dot h. And then we have to define 2 variables here, 2
global variables. I would say VHD address, and this was, in hex 38, I
think, if I remember correctly. And we make a buffer here as we did
before with the PH1750. And we need 6 indexes here, I think.
Let me see if this is correct. 1, 2, 3, 4, 5, 6. Yes. The 7th is not
interesting for us. we could implement it, but, also the DF robots
didn't do it.
So let's skip this one. Okay. Then in the setup, as we did before, wire
dots begin so that the E squared C communication could be started.
Now let's do some magic. The first magic is also in the previous
example, you should know that wire dot, begin transmission, and
then we are sending out the DHT address. And this should be here,
let me see this first example.
So we're sending out the DHT in the right models, then if we are
getting back here to acknowledge, this is all done by the wire dot
edge, then we are writing out here the 0xac commands. wirewire.
Right, then I copy and paste this two times because I want to write
33, and I would like to write 00. afterwards, we have to wire and
transmission. And we are not finished.
checking back to the flow because yes, it's stated out. Weights
80,000,000 seconds for the measurement to be called bleed. And
this is, we should also do that, and I would like to use 90
milliseconds here, and then the next flow can be started by reading
out the values. And this is also an easy part because we already did
this one. So we are starting the wire request from the DHT address.
Then we are now in the reading part because, we are not not what
is, request from? So it should work now. Let me check the compiler. I
missed something here because we have to adhere to the size. So,
6 bytes, I am expecting it.
Now, this should be looking a little bit better. So just run the compiler,
then also the intelli sense shows you that there is no error anymore.
And now we want to get here 6, 6 bytes. So we could do that, 1 by 1,
or we may hear a wire dot available, for example, and inside this
while we could make 4 here. So let's see if we can make an auto-
completion.
Completion. Let's check it out here. 4. Yes. And we're counting this
one.
For example, e, should be less and 6 so that we get 6 values. Size
could be the same here, and this is our code. And then we say
buffer. So we don't have to write it six times here with the index, I am
equal to wire dot read. it's a more convenient way and not writing
here six times here, the buffer element.
Okay. So let's test the first thing if we are really making progress
here or not, and I have already prepared this. So just checking out if
we're getting out here the first three bytes not with the serial index
because I just want to have the humidity level here. Then, uploading
the code. So I'm getting a lot of them here.
Whom well year equals to, what was the calculation? Calculation
was the come volume, this will automatically translate it or
transferred into, decimal volume divided to the power, 2 of 20 here.
So we have the first part, and then we are seeing multiply 100
looking good. Then we have a serial print here. And let's test it out,
and then we are going through the details of this very sexy line.
So let's check it out. Then I also would like to run a pulse. Then once
again, open up the line so that we have really the same data, check,
click on stop, So we have the humidity here by 50. Looks promising,
but let's see what pulse fuel says here. So here we have the first
command, the trigger commands, that, nothing new for us. And here
we have the state, and this should be now our new value.
So let's focus on what I did with this line. So, grab here on the
Blackboard once again so that we can do this step by step. So that
means those two bytes and this nimble should be now combined to
one value. And how could I do that with the humidity in three steps?
The first step is we are getting the first byte.
So these are 7 ones because the first one is a 0. So here we are. 1,
2, 3, 4, 5, 6, 7. And I have to shift it twelve times to the left because I
have 8 and 4 is 12, I need space for 12 more numbers. Then there
comes an or.
For the second number, these are 4, 8 ones. 341234. And I have to
shift those values four times to the left because I need space for the
extracted bits for the 4 of this one. And these are also 4 ones. And
this is now my new value.
So that means I have, in the first line, these 7 ones, 4, 5, 6, 7, and
then 12 0s. So that means for 8 and the other 4. And I made an or
with those values. So that means I have 1111 111 1. And those 4
should also have space for the next one.
So I am here at 1111. A lot of ones look really a little bit strange, but
it will give us the right value. So in summary, I have here 71 1, 2, 3,
4, 5, 6, 7. Then speeding up here, I have 12 times 1. So let's try out
here in the binary.
We have here 1, 2, 3, 4, 5, 6, 7, 8, 9, 10. 112. Enter binary to
decimal divided by 2 What was the calculation it is divided by? Was
this right to the power 20 divided by 2 to the power of 20 and 100. So
we have here the value of 50 in the humidity percent of humidity.
Nice one. Really nice one. Was it really a little bit distracted by all of
the ones here in our explanation, but it looks promising so far. Okay.
Now, we could do the same thing with the temperature.
And I would invite you to click here on pause and try it on your own
because it's a little bit different now. So I hope you tried your own
approach. And now, I would like to show you how to do it with the
temperature values. So first of all, we have to mask the last four.
This is also covered in our general, in our 1st general chapter
because the first four refer to the humidity and days last 4 will be
needed for our temperature. So masking out is extracted bits 2, for
example, we need the buffers here.
Should it be a 012 3. And now, we make a mask with an ant, 0f, in
hex, tilt a tiny version of it. And then we'll make the long temperature
equal to first, we start with the extracted bits, then we make a bit shift
and how much do we need space here. So these are the first
extracted bits. Then we have 88 that means 16 spaces we need
before 2 full bytes, then we make an or, then we have the buffer 4,
and we shift the 8 to the left because we need 8 more spaces.
Then we made an or, and then we say Buff 5. what do you think?
Seems legit and comprehensible. Then we are making a float here,
the temperature value equals the temperature divided by the power
of 2 20 multiplies. Let's do it outside the brackets.
So you can check out what's all about it. And keep in mind with 10
bits you have the ability to go up to 1024 theoretical devices. You
always have to think about why this is necessary in which use case?
Is it really necessary that I have 1000 devices in, my Arlino
command addressed, but maybe you have some use cases. And,
therefore, you can check it out.
Also, another reference I would like to show you is the adafruit
square dc address list, which ator food makes really a lot of sensors.
And also the clones from that are using the same e square address
and defragments scroll a little bit down and can see where and what
e squared c scheme is used for what kind of purpose. You can
define your own e squared to address, what we did in the example
before with the arduino and arduino communication. And in the next
step, we want to create our own sketch where we find out e squared
c addresses in an existing sketch because this could probably be
handy. when we want to investigate an existing circuit, what is this,
what kind of e squared c device is that, what is the address,
etcetera.
CREATE OWN SKETCH
FOR READING I2C
ADDRESSES
In this project, I would like to create with you and Sketch so that we
can find out all e squared c addresses in an existing circuit. So here
we have our Sketchform before with 3 devices, and I would like to
get here all of the e squared c addresses. And this could be handy,
as I mentioned before. Maybe you have an existing circuit. You don't
really know the e squared c address and just want to use the library,
etcetera.
So our first device was 23. So let's switch here a little bit to the right.
22 is not acknowledged. 23 is acknowledged because it's there. And
this same should be done by 38.
Yes, Otis is not acknowledged. And this is what we do here. We are
sending you the raw bytes for the address for the e squared c
address and waiting if we are getting back and acknowledged or not.
Easy as it is to understand how the whole E squared C
communication works and how easily we can create -- our own e
squared scanner.
I2C MULTIPLEXER
One last thing, and then we are at the end of this chapter, I would
like to show you an e squared c multiplexer. And this can be used
when you need from the same device, a few of them in 1 of your
circuits. Because if you want to use 2 of these, OLEDs, for example,
then this is not really easy to do because so let's see if they're, if I'm
getting in focus because the e squared c address stands here on this
side, 0, 3 c, we can solder out this little resistor to the other side and
then we're getting here an circuit because they have the same e
squared c address. With this multiplexer, as the multiplexer says,
they can handle the communication for you, giving them an internal
address. And so you can use 7 devices with 1 multiplex here and
you can cascade them.
So you could have many more devices here. And how you can use it
here, I refer to a really good site from random notes to terrors. And
this will show you how you can use this multiplex or this DCA. You
can find all of these devices in my component list with some links on
it. And when I go down, for example, creating multiple OLED
displays or also using, for example, multiple BM sensors, like we can
see here and with this multiplexer, it's easy to do it with only one
microcontroller.
So keep in mind, you need more e squared c from the same type,
use a multiplexer.