Posts

Showing posts from January, 2026

Most Common HAL Functions to use in STM32

Image
 ADCs 1.  HAL_ADC_Start( &hadc1) - Enables ADC and starts conversion of the regular channels. Replace &hadc1 with whatever ADC you are using 2. HAL_ADC_PollForConversion (&hadc1,100) Polls for regular conversion complete. 100 is the timeout in ms. beyond this time the function will timeout 3. ADC_Val = HAL_ADC_GetValue(& hadc1 ) Gets the data from ADC Data Register. Should be called after Polling 4. HAL_ADC_Stop (&hadc1) Disables ADC and stop conversion of regular channels. 5. HAL_ADC_Start_IT(&hadc1) -Enables the interrupt and starts ADC conversion of regular channels 6. void HAL_ADC_ConvCpltCallback ( ADC_HandleTypeDef *hadc) - Callback function when ADC has finished a conversion. Tips: Sometimes you need to slow down the clock of ADC by using maximum prescaler because the ADC runs into overrun issues(when CPU is presented with more samples than the CPU can process) Also you may need to start ADC again HAL_ADC_Start_IT(&hadc1) at the end...

Embedded Interview Questions

1. Why to use a volatile designator for a variable?   The volatile keyword is one of the most important concepts in embedded C, and using it incorrectly is a top cause of "impossible to find" bugs in STM32 projects. In your capacitance meter, variables like capture_done are updated inside an Interrupt Service Routine (ISR) —specifically the HAL_ADC_LevelOutOfWindowCallback —but read in your main loop . 1. The "Smart" Compiler Problem Compilers are designed to make your code as fast as possible. If a variable is checked in a loop but never modified inside that loop, the compiler assumes its value will never change. Without volatile : The compiler looks at your while(!capture_done); loop and thinks: "I see that capture_done is 0. Nothing in this loop changes it to 1. Therefore, I will just load the value '0' into a CPU register once and check that register forever." Even when your interrupt actually changes capture_done to 1 in the background m...

Control a RC car using Node-Red GUI and HiveMQ broker on PicoW

Image
 Node-Red Flow The cyan node is button node from Dashboard 2.0 which needs to be installed in node-red additionally as it is a 3rd party app. The pink node is mqtt out node. HiveMQ Take all the cluster details from HiveMQ to be put in mqtt out node Node-Red Button node settings Change the payload value for Left, Right, Front, Back and Stop Button to L, R, F, B & S value and select the type to be string Node-Red mqtt out node settings Click on the pencil button to set the broker settings First you need to arrange the buttons on the dashboard so that it intuitively can tell which button to press. so we need to rearrange the layout so that the buttons look like below on http://localhost:1880/dashboard In Dasboard 2.0 under pages hover mouse on the page . For me it is Garden , when i hover my mouse there appears a pencil shaped button . Click on it to go to layout editor Make the buttons 1x1 size and add spacer as shown in the layout below and save the layout. Testing connecti...

Control LED on PicoW using Node-Red and HiveMQ (broker)

Image
 To switch the onboard LED of a Raspberry Pi Pico W using Node-RED over the internet, the most robust method is using the MQTT protocol. This involves a "Broker" (a middleman in the cloud) that allows your Node-RED dashboard to talk to the Pico W regardless of where they are physically located. 1. T he "Middleman": Set up a Cloud MQTT Broker Since you want to control it via the internet , you need a public broker. HiveMQ Cloud or EMQX Cloud are great free options for small projects. Sign up and create a "Cluster." Create a Username and Password in the "Access Management" section. Note down your Broker URL (e.g., xxxxxx.s1.eu.hivemq.cloud ). 2. The Pico W: MicroPython Code You need the umqtt.simple library. In Thonny, go to Tools > Manage Packages and search for umqtt.simple to install it. Save the following as main.py on your Pico W: Python import network import time from machine import Pin from umqtt.simple import MQTTClient ...