{"id":3520,"date":"2025-06-12T12:51:01","date_gmt":"2025-06-12T12:51:01","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=3520"},"modified":"2025-06-12T12:51:04","modified_gmt":"2025-06-12T12:51:04","slug":"emulating-i2c-sensors-part-1-introduction-and-environment-setup","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=3520","title":{"rendered":"Emulating I2C Sensors Part 1: Introduction and Environment Setup"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"414\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/Untitled-1024x414.jpg\" alt=\"\" class=\"wp-image-3521\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/Untitled-1024x414.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/Untitled-300x121.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/Untitled-768x311.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/Untitled-1150x465.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/Untitled-750x303.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/Untitled-400x162.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/Untitled-250x101.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/Untitled.jpg 1365w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Emulating an I2C sensor using an STM32 microcontroller allows developers to simulate the behavior of real sensors for testing or development purposes. By configuring the STM32 as an I2C slave device, it can respond to read and write operations just like a physical sensor would on the bus.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>This guide shall cover the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Introduction.<\/li>\n\n\n\n<li>I2C Slave Configuration.<\/li>\n\n\n\n<li>I2C Master Configuration.<\/li>\n\n\n\n<li>Connection.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. Introduction:<\/h2>\n\n\n\n<p>Emulating an I2C sensor using an STM32 microcontroller is a powerful technique that allows developers to replicate the behavior of real-world I2C-based sensors for testing, prototyping, debugging, or educational purposes. Instead of connecting an actual physical sensor to an I2C bus, the STM32 microcontroller itself takes on the role of the sensor, responding to commands and queries from an I2C master (typically a microcontroller, microprocessor, or development board like Arduino or Raspberry Pi). This approach is particularly valuable in scenarios where the actual sensor hardware is unavailable, too costly, still under development, or when stress-testing and validation of an I2C master\u2019s handling logic is required.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">What is I2C and Why Emulate It?<\/h4>\n\n\n\n<p>I2C (Inter-Integrated Circuit) is a widely-used, two-wire serial communication protocol designed for short-distance communication between integrated circuits. It consists of two lines:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>SCL (Serial Clock Line)<\/strong>: Provides the clock signal generated by the master.<\/li>\n\n\n\n<li><strong>SDA (Serial Data Line)<\/strong>: Used for bidirectional data transfer.<\/li>\n<\/ul>\n\n\n\n<p>I2C supports multiple slave devices on the same bus, each identified by a unique 7-bit or 10-bit address. Sensors such as accelerometers, gyroscopes, temperature sensors, and EEPROMs commonly use I2C for data communication.<\/p>\n\n\n\n<p>Emulating such sensors on an STM32 microcontroller enables a developer to mimic a full I2C device&#8217;s response behavior, including address recognition, data transfers, and register-level interactions. This is not only useful for embedded systems testing but also for demonstrating how sensors operate internally.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Hardware Requirements<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>STM32 Microcontroller<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Select an STM32 MCU that supports I2C hardware in slave mode (e.g., STM32F1, F4, L4, H7 series).<\/li>\n\n\n\n<li>It should have sufficient I\/O capabilities, clock speed, and available peripherals to handle I2C traffic and manage emulated sensor logic.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>I2C Bus Compatibility<\/strong>:\n<ul class=\"wp-block-list\">\n<li>The STM32 must be physically connected to the I2C master via two GPIOs configured for I2C: one for SCL and one for SDA.<\/li>\n\n\n\n<li>These lines should support open-drain configuration, as per the I2C specification.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Pull-up Resistors<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Both SCL and SDA lines must be pulled up to the supply voltage (typically 3.3V or 5V) using appropriate resistors (usually 4.7 k\u03a9 to 10 k\u03a9).<\/li>\n\n\n\n<li>The STM32 and the master device must share a common ground reference.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Clock Synchronization<\/strong>:\n<ul class=\"wp-block-list\">\n<li>The STM32, when acting as an I2C slave, must be able to synchronize to the master\u2019s clock.<\/li>\n\n\n\n<li>The slave cannot drive the clock line but must respond in synchrony with it.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Electrical Considerations<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Ensure voltage level compatibility between the master and STM32. If the STM32 operates at 3.3V and the master at 5V, use level shifters or ensure the master supports 3.3V I2C logic levels.<\/li>\n\n\n\n<li>Pay attention to I2C bus capacitance, especially when connecting multiple devices or using long wires.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">Functional Requirements of an Emulated Sensor<\/h4>\n\n\n\n<p>To effectively emulate an I2C sensor, the STM32 must mimic:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>I2C Address Recognition<\/strong>: Responding only to its assigned address.<\/li>\n\n\n\n<li><strong>Register Map Emulation<\/strong>: Simulating internal registers, such as configuration registers, data output registers, and identification registers like\u00a0<code>WHO_AM_I<\/code>.<\/li>\n\n\n\n<li><strong>Read\/Write Behavior<\/strong>: Supporting correct behavior when the master reads or writes to specific registers.<\/li>\n\n\n\n<li><strong>Timing Constraints<\/strong>: Responding within required I2C bus timing limits to avoid errors.<\/li>\n\n\n\n<li><strong>Optional Features<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Implementing repeated starts, clock stretching (if needed and supported), or even simulating interrupt outputs via GPIO for &#8220;data ready&#8221; signals.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">Use Cases and Benefits<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Firmware Development<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Test I2C master firmware without needing physical sensors.<\/li>\n\n\n\n<li>Verify register-level communication handling and error recovery logic.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Education and Demonstration<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Teach I2C communication by showing both master and slave implementations.<\/li>\n\n\n\n<li>Use in lab experiments where replicating sensor output can be controlled manually.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Debugging and Validation<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Emulate sensors to reproduce specific edge cases or faults (e.g., constant readings, corrupted data).<\/li>\n\n\n\n<li>Validate system behavior when the sensor is absent or responds incorrectly.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>System Simulation<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Build virtual hardware prototypes.<\/li>\n\n\n\n<li>Create simulation environments that replicate production systems.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. I2C Slave Configuration:<\/h2>\n\n\n\n<p>We start off by creating new project as follows:<\/p>\n\n\n\n<p>Open STM32CubeIDE after selecting the workspace and create new project as following:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"355\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-26-00-1024x355.jpg\" alt=\"\" class=\"wp-image-2940\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-26-00-1024x355.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-26-00-300x104.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-26-00-768x266.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-26-00-1536x532.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-26-00-2048x709.jpg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-26-00-1150x398.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-26-00-750x260.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-26-00-400x139.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-26-00-250x87.jpg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Select the MCU:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"832\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-27-14-1024x832.jpg\" alt=\"\" class=\"wp-image-2941\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-27-14-1024x832.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-27-14-300x244.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-27-14-768x624.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-27-14-1536x1248.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-27-14-2048x1664.jpg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-27-14-1150x934.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-27-14-750x609.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-27-14-400x325.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/10\/2024-10-26_16-27-14-250x203.jpg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Give the project a name:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"813\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-13-07-813x1024.jpg\" alt=\"\" class=\"wp-image-3522\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-13-07-813x1024.jpg 813w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-13-07-238x300.jpg 238w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-13-07-768x968.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-13-07-750x945.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-13-07-400x504.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-13-07-250x315.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-13-07.jpg 984w\" sizes=\"(max-width: 813px) 100vw, 813px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Make sure the Targeted Project Type is STM32Cube, Language is C and binary type is Executable.<\/p>\n\n\n\n<p>After that, set PB8 and PB9 for I2C1 or I2C3, here we shall use I2C1 as following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"972\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-18-06-1024x972.jpg\" alt=\"\" class=\"wp-image-3523\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-18-06-1024x972.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-18-06-300x285.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-18-06-768x729.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-18-06-1536x1457.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-18-06-1150x1091.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-18-06-750x712.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-18-06-400x379.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-18-06-250x237.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-18-06.jpg 1756w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Next, we shall configure I2C as follows:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Set I2C mode to I2C.<\/li>\n\n\n\n<li>Set Primary slave  address to 0x1D.<\/li>\n\n\n\n<li>Leave the rest of parameters as is.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"993\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-15-12-993x1024.jpg\" alt=\"\" class=\"wp-image-3524\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-15-12-993x1024.jpg 993w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-15-12-291x300.jpg 291w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-15-12-768x792.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-15-12-1490x1536.jpg 1490w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-15-12-1150x1186.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-15-12-750x773.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-15-12-400x412.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-15-12-250x258.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-15-12.jpg 1926w\" sizes=\"(max-width: 993px) 100vw, 993px\" \/><\/figure>\n\n\n\n<p>Next, Enable interrupt for i2c as follows:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>I2C1 event interrupt.<\/li>\n\n\n\n<li>I2C1 error interrupt.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"849\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-16-41-1024x849.jpg\" alt=\"\" class=\"wp-image-3525\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-16-41-1024x849.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-16-41-300x249.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-16-41-768x636.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-16-41-1536x1273.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-16-41-1150x953.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-16-41-750x621.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-16-41-400x331.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-16-41-250x207.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-16-41.jpg 1844w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Next, from GPIO Settings, enable Pullup as follows:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1021\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-17-00-1021x1024.jpg\" alt=\"\" class=\"wp-image-3526\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-17-00-1021x1024.jpg 1021w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-17-00-300x300.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-17-00-150x150.jpg 150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-17-00-768x770.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-17-00-1531x1536.jpg 1531w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-17-00-1150x1154.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-17-00-750x752.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-17-00-400x400.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-17-00-250x250.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-17-00.jpg 1950w\" sizes=\"(max-width: 1021px) 100vw, 1021px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Thats all for the slave configuration. Save the project and this will generate the code.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Master Configuration:<\/h2>\n\n\n\n<p>After selecting the MCU, give the project a name:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"813\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-20-46-813x1024.jpg\" alt=\"\" class=\"wp-image-3528\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-20-46-813x1024.jpg 813w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-20-46-238x300.jpg 238w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-20-46-768x968.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-20-46-750x945.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-20-46-400x504.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-20-46-250x315.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-20-46.jpg 984w\" sizes=\"(max-width: 813px) 100vw, 813px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Make sure the Targeted Project Type is STM32Cube, Language is C and binary type is Executable.<\/p>\n\n\n\n<p>After that, set PB8 and PB9 for I2C1 or I2C3, here we shall use I2C1 as following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"838\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-21-32-1024x838.jpg\" alt=\"\" class=\"wp-image-3529\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-21-32-1024x838.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-21-32-300x246.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-21-32-768x629.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-21-32-1536x1257.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-21-32-1150x941.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-21-32-750x614.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-21-32-400x327.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-21-32-250x205.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-21-32.jpg 1718w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Next, configure the I2C as follows:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"895\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-21-54-1-895x1024.jpg\" alt=\"\" class=\"wp-image-3531\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-21-54-1-895x1024.jpg 895w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-21-54-1-262x300.jpg 262w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-21-54-1-768x878.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-21-54-1-1343x1536.jpg 1343w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-21-54-1-1150x1315.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-21-54-1-750x858.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-21-54-1-400x457.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-21-54-1-250x286.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-21-54-1.jpg 1614w\" sizes=\"(max-width: 895px) 100vw, 895px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Leave everything as default since we are planning to use polling mode to read from salve I2C.<\/p>\n\n\n\n<p>Next, enable USART2 to use with printf later in the guide. To enable USART:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"841\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-22-14-1-841x1024.jpg\" alt=\"\" class=\"wp-image-3534\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-22-14-1-841x1024.jpg 841w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-22-14-1-246x300.jpg 246w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-22-14-1-768x935.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-22-14-1-1261x1536.jpg 1261w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-22-14-1-1150x1401.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-22-14-1-750x913.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-22-14-1-400x487.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-22-14-1-250x304.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-22-14-1.jpg 1524w\" sizes=\"(max-width: 841px) 100vw, 841px\" \/><\/figure>\n\n\n\n<p>Leave everything as is.<\/p>\n\n\n\n<p>Thats all for the Master configuration. Save the project and this will generate the code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Connection:<\/h2>\n\n\n\n<p>The connection as follows:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"594\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-46-50-1-1024x594.jpg\" alt=\"\" class=\"wp-image-3536\" style=\"width:840px;height:auto\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-46-50-1-1024x594.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-46-50-1-300x174.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-46-50-1-768x446.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-46-50-1-1536x892.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-46-50-1-2048x1189.jpg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-46-50-1-1150x668.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-46-50-1-750x435.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-46-50-1-400x232.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-12_15-46-50-1-250x145.jpg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>Pin<\/td><td>STM32F4 Master<\/td><td>STM32F4 Slave<\/td><\/tr><tr><td>P8<\/td><td>PB8<\/td><td>PB8<\/td><\/tr><tr><td>PB9<\/td><td>PB9<\/td><td>PB9<\/td><\/tr><tr><td>GND<\/td><td>GND<\/td><td>GND<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Stay tuned for part 2 for communication.<\/p>\n\n\n\n<p>Happy coding \ud83d\ude09<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Emulating an I2C sensor using an STM32 microcontroller allows developers to simulate the behavior of real sensors for testing or development purposes. By configuring the STM32 as an I2C slave device, it can respond to read and write operations just like a physical sensor would on the bus. This guide shall cover the following: 1. [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,11,12],"tags":[],"class_list":["post-3520","post","type-post","status-publish","format-standard","hentry","category-embedded-systems","category-peripheral-drivers","category-stm32"],"_links":{"self":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/3520"}],"collection":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3520"}],"version-history":[{"count":3,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/3520\/revisions"}],"predecessor-version":[{"id":3537,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/3520\/revisions\/3537"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3520"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3520"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3520"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}