Getting Started With STM32 and Nucleo Part 2: How to Use I2C to Read Temperature Sensor TMP102

  Переглядів 251,327

DigiKey

DigiKey

4 роки тому

Let’s take our STM32 to the next level and communicate with a sensor. We’ll use the I2C protocol to send and receive messages with a TI TMP102.
In this video, we’ll show you how to use the STM32CubeIDE to create a new project and write a few simple STM32 HAL commands to transmit messages across the I2C bus. We’ll read temperature data from a TMP102 sensor and then calculate a readable temperature in Celsius from that information. Finally, we’ll use the STM32’s UART peripheral to transmit that temperature data back to our computer to read on a serial console.
The STM32 series is based on the ARM Cortex architectures. Specifically, we’ll be looking at the STM32 Arm Cortex-M microcontrollers. To make life easier, we’ll be using STMicroelectronics’ official Nucleo development boards (the Nucleo-L476RG in particular).
WRITTEN TUTORIAL
If you would like to see the steps performed in this video in written form, please see the tutorial here: www.digikey.com/en/maker/proj...
Wishlist (shared cart): www.digikey.com/short/p59jrr
ADDITIONAL RESOURCES
Related Projects www.digikey.com/maker/search-...
Download STM32CubeIDE: www.st.com/en/development-too...
STM32 HAL documentation (click on family from graphic and then Resources tab) : www.st.com/en/embedded-softwa...
SparkFun I2C Tutorial: learn.sparkfun.com/tutorials/...
Maker.io: www.digikey.com/en/maker
Digi-Key’s Blog - TheCircuit: www.digikey.com/en/blog
Connect with Digi-Key on Facebook: / digikey.electronics
And follow us on Twitter: / digikey

КОМЕНТАРІ: 135
@igabo95
@igabo95 3 роки тому
Man your explanations are perfect. It's just the right rythme I need, thank you !
@shambles9
@shambles9 Рік тому
These videos are the gift that keeps giving. I used this very video over a year ago to help me get up to speed and it was invaluable today as well. Thanks Shawn!! printf and its derivatives are capable of printing floating point numbers by the way, no need to juggle the number and print the integer and fractional parts separately.
@DownhillAllTheWay
@DownhillAllTheWay 4 роки тому
I'd really like to see your simple, concise explanation for reading and writing to an SD card with SPI. That would also solve other stuff I want to do with SPI.
@gustavoghedim3047
@gustavoghedim3047 4 роки тому
The best explanation I've found so far, one video about SPI and CAN communication would be amazing!
@caffeinatedinsanity2324
@caffeinatedinsanity2324 3 роки тому
CAN deserves some explanation indeed. As for SPI, I always regarded it as one of the simplest protocols out there, more than UART, since there's almost no device link data structuring (like UART with start, parity or stop bits). The data structure is probably only dictated by the type of device you want to communicate with.
@shelbypereira
@shelbypereira 10 місяців тому
these videos are brilliant, so clear , to the point and highlights all the important stuff including error handling... and I love the bow tie!
@dnyaneshvarsalve
@dnyaneshvarsalve Рік тому
Your explanation to every topic is very good and easy to understand. Thank you
@moinshaikh6684
@moinshaikh6684 3 роки тому
Very very nice explaination dear! You're the Reason I understand the programming of stm for the first time.
@ValiRossi
@ValiRossi Рік тому
I just bought 3 STM32 Disco boards from Digikey. These videos are great. Thanks Digikey!
@kumarghz
@kumarghz 4 роки тому
Amazing explanation about I2C. Please make one for SPI
@nogaavraham7173
@nogaavraham7173 2 роки тому
As always, great tutorial! Very helpful. Thanks alot!👍
@damny0utoobe
@damny0utoobe 3 роки тому
Very nice to see digikey getting into the STM32 tutorial game.
@David-yp9oz
@David-yp9oz 4 роки тому
Awesome vid, can you make one for SPI as well?
@jamesdavid5224
@jamesdavid5224 3 роки тому
Bro, these tutorials as a refresher from my final year course has been a life saver! I can't thank you enough!
@Everflamen
@Everflamen 2 роки тому
What did you study? :D
@jamesdavid5224
@jamesdavid5224 2 роки тому
@@Everflamen Electrical Engineering
@amulshinde6102
@amulshinde6102 3 роки тому
Thank you very much, this video helped me before breaking my neck. Saved my time.
@rubikon7648
@rubikon7648 3 роки тому
One of the best explanations, thank you
@filmweaver2013
@filmweaver2013 2 місяці тому
Excellent explanation! Even as a mechanical engineer with no previous formal knowledge on this, I am able to understand! Now I understand how "easy" arduino makes all this, for beginners
@michlin19710825
@michlin19710825 3 роки тому
Learned a lot thanks. Would be very helpful if you could add a similar tutorial video on SPI.
@rahaz7073
@rahaz7073 2 роки тому
Best teacher ever---Thanks
@volik2103
@volik2103 3 роки тому
Ethernet tutorial for sure will be awesome!
@thomaseslier5332
@thomaseslier5332 2 роки тому
Amazing tutorial! Thank you very much
@SKPang
@SKPang 4 роки тому
Great tutorial. Please make more.
@DidiCechet
@DidiCechet 3 роки тому
Very good explanation! Thanks a lot for it 👏
@paulg.3067
@paulg.3067 3 роки тому
Very informative and condensed video :)
@a.s.8984
@a.s.8984 4 роки тому
Hello Shawn, it´s the best tutorial I have ever seen. Thank You. I only have trouble with displaying the symbols on puTTY. In the device manager my board STM32L100 is not listed as Com Port, but as an USB-Device. Do I need a special driver?
@ramimehyar481
@ramimehyar481 2 роки тому
Please one similar for SPI, great video!!!
@piratesofbytes
@piratesofbytes 4 роки тому
WOW Super Tutorial !!!
@Graham1904
@Graham1904 4 роки тому
Always a pleasure to watch these tutorials :) Keep them coming.....
@nereamonrio7561
@nereamonrio7561 Рік тому
Are u an unit vector? Because you give my life direction
@adaminsanoff
@adaminsanoff 3 роки тому
I cannot thank you enough!
@Hirnlappen
@Hirnlappen 4 роки тому
Incredibly sharp and concise tutorial!
@ramimehyar481
@ramimehyar481 2 роки тому
Can you please explain to me how we sent the 8th bit to determine if we are writing or reading?
@Hirnlappen
@Hirnlappen 2 роки тому
@@ramimehyar481 at 12:20, you can see that there are two different functions for either writing to, or reading data from the I2C device. They will set the 8th bit accordingly.
@ramimehyar481
@ramimehyar481 2 роки тому
@@Hirnlappen thank you, i have assumed the same, so that means that this w/r bit is a standard i2c protocol to be at that location?
@Hirnlappen
@Hirnlappen 2 роки тому
@@ramimehyar481 Indeed. But just to be sure, I also looked it up again. You find it on the Wikipedia page as well as in the I2C standard, which is very well written (it's linked from the Wiki page).
@abutaymiyyahlectures
@abutaymiyyahlectures 2 роки тому
damn the smile at the end of every explanation is so awesome
@Metroid1890
@Metroid1890 3 роки тому
It's just the 2nd video and I already want to buy a Nucleo
@oswevega2939
@oswevega2939 Рік тому
excellent. very useful
@onszerai755
@onszerai755 2 роки тому
Thanks a bunch for the great explanation, i wonder if you have an example of SCD30 CO2 sensor or if y can guide me because i didn't get good results so please answer me asap! thanks
@Bob-zg2zf
@Bob-zg2zf Рік тому
excellent!
@flexplayz6551
@flexplayz6551 2 роки тому
This is amaizing.
@sachinshet4569
@sachinshet4569 4 роки тому
Which tool need to use simulate STM nucleo board and code.
@danndemetre6643
@danndemetre6643 2 роки тому
Great tutorial, used this in part to setup an ADS1115 connected to a LM60 on the BluePill. Just one thing though: sprintf((char*)buf, "%u.%02u C ", ((unsigned int)temp_c / 100), ((unsigned int)temp_c % 100)); } This will fix chopping of leading zeros for the decimal value. (ex: 20.09 = 20.09 and not 20.9)
@bhargavamgoth3286
@bhargavamgoth3286 8 місяців тому
I am using stm21f722ZET6U Nucleo board, I tried to program for simple LED Toggle and I selected the nucleo board and configured RCC and SYS and Selected in built LED in as output but when i try to generate the code for it its not generating the code
@1over137
@1over137 Рік тому
Thanks. I ran into a curious issues. After following this on a Nucleo-64 I added an OLED 128x64 1306 to the I2C bus. The debugger/programmer couldn't reach the MCU. The MCU was running, but hthe STLNK couldn't reset it. I reviewed the wiring and found by simply connecting the OLED to the 3,3V pin was enough to cause the programmer to fail. It makes no sense. I check the OLED on an ESP32 and it worked perfectly. It's almost as if the 3.3V regulator is overloaded with the chip+tmp102+oled.
@chakrarokaya3952
@chakrarokaya3952 28 днів тому
Nice explanation. A per my understanding we are reading two bytes [buf[0] and buf[1] from the temperature sensor. could any one please explain for the combining bytes buf[0] 4. Why left shit and right shift by 4?. Thanks
@fernandoi8958
@fernandoi8958 Рік тому
I would like to complement this amazing tutorial by pointing out a few issues: 1) the 2s complement comment should state that you are actually extending the 2s complement encoding from 12 bits to 16 bits; 2) the float value is converted to unsigned int in the printf, so negative values will not be displayed correctly.
@JasperHatilima
@JasperHatilima Рік тому
I still confused with that part of the code. I was expecting that the a value that is in 2s compliment would require several steps. The steps I would expect would be: checking if most significant bit is a 1. Then if true, invert all bits and add a one at lsb location. But here he just does logic OR with 0xF000. I am confused...kindly explain.
@fernandoi8958
@fernandoi8958 Рік тому
@@JasperHatilima 12:56 the temp sensor has only 12 bits, but val is a 16 bit signed integer (int16_t). Then, he checks if the most significant bit of the 12 bit temperature sensor reading is active by testing (val > 0x7FF). When he does the or-ing (val |= 0xF000) he is just extending the 16 bit variable with leading ones so that the whole 16 bit variable accurately represents a 2-complement value
@ramimehyar481
@ramimehyar481 2 роки тому
How does the mastertransmit and masterreceive toggle the last bit 1 or 0 to determine if we are writing or reading? I do not see how Hal_ could know this from the datasheet of the sensor! Otherwise, how then are we sending this last bit accordingly?
@fenderrexfender
@fenderrexfender 4 роки тому
so some of my USB ports don't put out enough current for this board. its a bit more hungry than some boards iv played with in the past. the board would disconnect at stlink updates
@pawelp.340
@pawelp.340 3 роки тому
Do I have to end debug session in order to receive anything from COM port? I'm not able to get any data via USB while in debug session.
@electronichome1153
@electronichome1153 Рік тому
Many thanks Shawn! I'm really motivated to finish this course, but already in this second lesson I'm running into the complexity of STM32 programming. It's not like the standard C to me, especially with those crazy HAL functions. Also, your programs are quite elegant and professional, which means in plain language that they are not friendly for C beginners, or at least not for me. But I don't give up and keep going.
@oswevega2939
@oswevega2939 Рік тому
hello ! i felt the same trying some code in RTOS for ESP8266 but guess what , nowadays everything is like this , and most of sw is copy paste.... so our good old days are over.... let´s get used to very high level of abstraction to use these tools unfortunately....
@abdulwasey1814
@abdulwasey1814 3 роки тому
u r legend
@kia2065
@kia2065 3 роки тому
How would this work with a gyroscope? That's what I'm trying to figure out.
@ABS_guy
@ABS_guy 3 роки тому
Could you make a tutorial for updating the virtual COM ports for Putty -> STM serial connections? Windows 10 has made the COM ports obsolete and its been impossible to debug. Tried 6 tutorials to fix the issue for COM ports and nothing works.
@gautambaskaran1907
@gautambaskaran1907 Місяць тому
I guess there is a confusion when performing 2s complement. I dont think this will work for negative temperature. Please correct me if wrong
@HellesMammuthBS
@HellesMammuthBS 3 роки тому
Super nice videos!!! Only thing that confuses me is your intergalactic talking speed ;)
@LiNa-ll3gz
@LiNa-ll3gz Рік тому
Is there any tutorial on I2C low level driver?
@axelcastrejon6730
@axelcastrejon6730 6 місяців тому
How did your code work when you had no pull up resistor? You didn't even set the internal one
@donprefontaine3237
@donprefontaine3237 3 роки тому
Superb videos (I only watched the first two) but product environment is extremely complex. The vendor needs to simplify the dev environment for new users or their only customers will be experienced engineers: that will not allow the vendor to increase sales. So... consider a tiered learning level from simple to moderate to complex. As a course developer/instructor for 23 years before retirement, I have found this to be a successful approach.
@riannata8309
@riannata8309 2 роки тому
hi friend, may you make tutorial in INA219 Current sensor please.
@tngdwn8350
@tngdwn8350 2 роки тому
Can somebody explain me why it's necessary to cast buf to a char pointer when using strcpy?
@havva61
@havva61 Рік тому
I did same things on anoteher card of stm32 from stm32F103 series but I got something wrong messages like ''huart2' undeclared (first use in this function)'. How can get rid of this problem? Do you have any idea?
@user-ww2lc1yo9c
@user-ww2lc1yo9c Рік тому
what could prevent UART from transmitting that we need to make use of HAL_MAX_DELAY?
@PrasannaRoutray97
@PrasannaRoutray97 2 роки тому
What about device like AD7147 that can be used to connect to 13 sensors? How to read using STM32? Example?
@erdoganseref5425
@erdoganseref5425 2 роки тому
I tried to do the same with the MPU6000 but it didn't work. I always Error Tx.
@Graham1904
@Graham1904 4 роки тому
Great video, well explained. I am a newbie to STM32.... What is HAL?
@David-yp9oz
@David-yp9oz 4 роки тому
Hardware Abstraction Layer www.datarespons.com/pros-cons-using-stm32cubemx-code-generation-tool-insead-manually-writing-drivers-arm-cortex-m-microcontroller/
@LinaLin-xq8pd
@LinaLin-xq8pd 4 роки тому
Hardware Abstraction Level - a layer of programming that allows a computer operating system to interact with a hardware device at a general or abstract level rather than at a detailed hardware level
@naasikhendricks1501
@naasikhendricks1501 4 роки тому
I would say Shaun's coding style is good.
@abutaymiyyahlectures
@abutaymiyyahlectures 2 роки тому
the diagram & schemetic is not there anymore for the nucleo-64 boards datasheet :( 2:11
@tinu5779
@tinu5779 2 роки тому
now after finishing this code it would have been interesting to add another device, maybe an SPI device and show how to do that without losing my own code.
@stm32user
@stm32user 4 роки тому
Hey man. Why you left shifted ?.! One time ?.
@komeleduc
@komeleduc 2 місяці тому
i tried to do this on my iks01a3 and everything looked fine until i only got the first 8 bits, the second 8 bits stayed the same as the first 8 bits (yes i print the combined 2-byte hex value). anyone like to discuss?
@user-ww2lc1yo9c
@user-ww2lc1yo9c Рік тому
where are the hi2c1 and huart2 declared?
@yosefgreeves8379
@yosefgreeves8379 Рік тому
¿Can run in an ESP32 board ?
@pseudounknow5559
@pseudounknow5559 2 роки тому
Why the address for reading/writing stay the same ? We should do something like this "TMP102_ADDR | 0x01" in order to add 1 ?
@ramimehyar481
@ramimehyar481 2 роки тому
Did you get an answer for this obvious question? I asked it again, i want to know the same! I guess the hal_ transmit and receive parameter does that, but i want to know how
@berktopbas1496
@berktopbas1496 3 роки тому
Why didn't we change last bit(R/W bit) of the TMP102 address from 0 to 1 in HAL_I2C_Master_Receive function in order to read data from the slave, as you mentioned in 09:20? I think we could easily do that by TMP102_ADDR | 0x01.
@mozhang2724
@mozhang2724 Рік тому
I have the same question
@_sk_beatz_6051
@_sk_beatz_6051 Рік тому
@@mozhang2724 0x01 is the adress of temperature register where in the hal i2c master transmit function it will take care of R/W bit if u using transmit function means R/W bit set to 0 and if u using Recive then it set to R/W bit set to 1
@_sk_beatz_6051
@_sk_beatz_6051 Рік тому
@@mozhang2724 and 0x01 is not R/W bit it is Temperature register adress
@itsme.6863
@itsme.6863 Рік тому
Why my browser insists in showing me subtitle for this video in Korean language???
@jackgreer8023
@jackgreer8023 Рік тому
Is there a reason why it's preferable to declare TMP102_ADDR and REG_TEMP as private variables instead of macros at around 10:40? Thanks!
@keistvin6263
@keistvin6263 Рік тому
so he can declare it as uint8_t and shift it
@rftech1608
@rftech1608 Рік тому
that was a lot to take in
@chrisblain7065
@chrisblain7065 5 днів тому
At 11:48, he does something to show the parameters for HAL_Status... I'm assuming that it's something from a right click menu, but I can't figure out what it is...
@chrisblain7065
@chrisblain7065 5 днів тому
It's F2... nevermind!
@snehamadhuri990
@snehamadhuri990 2 роки тому
your explanations are good but I've tried for HTS221 sensor and i'm getting always tx error even i changed the slave address. Can you please do a video on MEMS sensor (IKS01A2).
@komeleduc
@komeleduc 2 місяці тому
i think your address for temp sensor is different from 0x48 shown above. im doing the same for my stts751 on iks01a3
@adaminsanoff
@adaminsanoff 3 роки тому
Use *dmesg | grep tty* command in Linux to find the serial port.
@sscorp_learning7680
@sscorp_learning7680 10 місяців тому
I am getting "Error Tx". The UART worked with "Hello!".
@Mar-md8rc
@Mar-md8rc 10 місяців тому
Hello, me too.
@Mar-md8rc
@Mar-md8rc 10 місяців тому
Hey for me it works now. I got obly a smd chip of a temperature sensor. I pulled the data pins with 10kohms to vcc and now it works.
@edohachamo647
@edohachamo647 4 роки тому
can you please share the steps performed in this video in written form? i couldn't find it at the site
@digikey
@digikey 4 роки тому
Hello, Our Maker site is currently down for maintenance. Please check back in a couple of days. Sorry for the inconvenience.
@KennySanzYT
@KennySanzYT 11 місяців тому
With NUCLEO-F303RE I keep getting only ErrorTX, i'm sure my pins are in the right position and the code is correct, anyone know what the issue could be?
@Mar-md8rc
@Mar-md8rc 10 місяців тому
Me too :(
@ajeethsuryash5123
@ajeethsuryash5123 4 роки тому
This guy is a compiler by himself... He doesn't need any IDE
@davy822
@davy822 4 роки тому
I don't understand how you did the 2's complement
@imcnx1563
@imcnx1563 4 роки тому
Dave you add 1 to your binary number then flip all the numbers to 1 if they were 0 and 0 if they were 1. i.g. Your binary number is 1001011 so after adding 1 you’ll get 1001100 and finally you’ll have 0110011
@BobBeatski71
@BobBeatski71 4 роки тому
Dave is right. Line 129 sign extends the float value to correctly ensure the number is negative, but the additional code is missing. Temps
@ShawnHymel
@ShawnHymel 4 роки тому
val is an int16_t, so what we're doing is looking to see if the first bit of our 12-bit number is 1. If it is, we pad 0xF to the front of the 12-bit number (making it a 16-bit number that starts with b1111). Because it's a signed int16_t, the 2's complement will be automatically performed for us, resulting in the correct negative number. Hope that helps!
@BobBeatski71
@BobBeatski71 4 роки тому
Aaahhh. You C programmers are spoilt. ;) I'm an assembly guy and we (generally) have to do it the long handed method: ori #$F000, reg1 not reg1 addi #1, reg1
@ajayrajan8882
@ajayrajan8882 3 роки тому
@@BobBeatski71 minimizing a long task with some abstraction doesn't mean they are spoilt :)
@soranfatah1033
@soranfatah1033 Рік тому
you spoken very speed
@abutaymiyyahlectures
@abutaymiyyahlectures 2 роки тому
mine keeps giving me error :/
@realdragon
@realdragon 4 роки тому
I don't like it, it jumped from hello world to operating bytes
@yanhuiyuan5686
@yanhuiyuan5686 4 роки тому
very good tut. can u share the code?
@yanhuiyuan5686
@yanhuiyuan5686 4 роки тому
found it on digikey website, thx
@edohachamo647
@edohachamo647 4 роки тому
@@yanhuiyuan5686 where did you find the code? can you share the link, i couldn't find it in the site
@edohachamo647
@edohachamo647 4 роки тому
@@yanhuiyuan5686 hi, can you share the link? i couldnt find it in the site
@yanhuiyuan5686
@yanhuiyuan5686 4 роки тому
www.digikey.com/en/maker/projects/getting-started-with-stm32-i2c-example/ba8c2bfef2024654b5dd10012425fa23
@nsashan
@nsashan Рік тому
korean subtitles?
@AnhTuNguyenanhtunguyen
@AnhTuNguyenanhtunguyen 4 роки тому
Hi, firstly thank you very much for the useful video. But I still got a question. In my case, the result looks something like error tx or rx between the actual temperature. Here is the picture imgur.com/djMx7oE
@abutaymiyyahlectures
@abutaymiyyahlectures 2 роки тому
you are not alone
@AnhTuNguyenanhtunguyen
@AnhTuNguyenanhtunguyen 2 роки тому
@@abutaymiyyahlectures did you find solution?
@abutaymiyyahlectures
@abutaymiyyahlectures 2 роки тому
@@AnhTuNguyenanhtunguyen actually, yes i did
@abutaymiyyahlectures
@abutaymiyyahlectures 2 роки тому
@@AnhTuNguyenanhtunguyen the problem for me was, the pins, u know how there are 2 rows of pins for the nucleo right?, i addidentally put the wire on the first row instead of the second row. Its my first time making this mistake. How i figured out my mistake is by making use of the other i2c pins, there are 4 total pins for i2c1, u can use either one of the pairs, for me the first one didnt work because of the pin mistake, so i decideded to use the second pair, then it worked and i found my mistake, hope this helps, u can try making use of i2c2 as well if u have
@tomfranky1744
@tomfranky1744 2 роки тому
Too complicated for "getting started part 2"!
@tm-uz7md
@tm-uz7md 2 роки тому
Ridiculous complexity. Just use an Arduino, the libraries do 95% of it for you.
@wesleydou337
@wesleydou337 7 місяців тому
N'est video ever!
@wesleydou337
@wesleydou337 7 місяців тому
Best
@matovupaul4942
@matovupaul4942 Рік тому
You too fast and the quality of the videos is not good. Hope you could do it like Paul McWhorter in his Arduino tutorials it would be better
@riannata8309
@riannata8309 2 роки тому
hi friend, may you make tutorial in INA219 Current sensor please.
@riannata8309
@riannata8309 2 роки тому
hi friend, may you make tutorial in INA219 Current sensor please.
Лизка заплакала смотря видео котиков🙀😭
00:33
😱СНЯЛ СУПЕР КОТА НА КАМЕРУ⁉
00:37
OMG DEN
Переглядів 708 тис.
STM32 Guide #1: Your first STM32 dev board
12:12
Mitch Davis
Переглядів 186 тис.
How I2C Communication Works and How To Use It with Arduino
9:57
How To Mechatronics
Переглядів 1,5 млн
PROTOCOLS: UART - I2C - SPI - Serial communications #001
11:58
Electronoobs
Переглядів 1,3 млн
Electronic Basics #19: I2C and how to use it
6:09
GreatScott!
Переглядів 409 тис.
STM32 Guide #4: Generated Code, HAL, and Bare Metal
26:20
Mitch Davis
Переглядів 73 тис.
STM32 DMA and FreeRTOS Tutorial - Phil's Lab #14
29:10
Phil’s Lab
Переглядів 106 тис.
Easy & Powerful Arduino Alternative? STM32 Beginner's Guide
9:49
GreatScott!
Переглядів 1 млн
Result of the portable iPhone electrical machine #hacks
1:01
KevKevKiwi
Переглядів 7 млн
Как открыть дверь в Jaecoo J8? Удобно?🤔😊
0:27
Суворкин Сергей
Переглядів 976 тис.