Ground wire with multiple batteries

Maybe it's stupid to ask this question:

Should I connect all ground wire together with multiple different voltage batteries?

Don't know if this is correct, I never connect batteries ground wire all together when I build such robots. I build a machine with 24V for motors and 12V for other sensors, light etc. Recently I keep hearing about ground wire should connected together in one machine. I am a bit confusing since I don't know much about electronics. Am I hearing the correct information? It won't cause any shortage, sparks or even burned?

Someday I migh build a machine which using 36V, 24V,12V and 5V, shall I connect the ground all together for all batetries and devices sensors etc?

In most cases the answer is

In most cases the answer is yes - you should connect all the grounds together regardless of voltage differences.
Having a common ground means that your circuit has a common point of reference, which is especially important when components are sending signals to each other. Voltage is a relative measurement, so you need a reference that is common for the signals to make sense.

Say you have a sensor that runs on 5V and a micro that runs on 3.3V. They both have their own separate power supplies, and the micro needs to read the sensor’s digital output for the circuit to work. When the sensor’s output goes high (let’s say +5V), that means the sensor’s output pin is 5V higher than the sensor’s ground.
From the perspective of the micro, what voltage is the sensor sending it? If the grounds are not commoned together, you don’t actually know if they’re at the same voltage level. Also, since there’s no path for the current from the sensor’s output to return back to the sensor’s ground, the sensor can’t actually drive the micro’s input to 5V.
When you common the grounds however, the sensor can drive the micro’s input to 5V relative to both the sensor’s ground and the micro’s ground - in other words, both components will see the expected 5V.

In some cases you don’t need to use common ground, but usually it’s more convenient to common everything up anyway. They aren’t many situations where you’ll actually cause a problem by connecting the grounds, usually you only need to isolate them for noise or surge protection.

Building on TeleFox’s

Building on TeleFox’s comment the only time I don’t connect ground is if I have a separate battery to drive motors. Motors are noisy and I want to make sure that none of that noise transfers to the logical circuitry. Connecting the motor ground wouldn’t break anything but might cause the ground to jump up and down in response to the motors starting up and stopping.

Thanks! I will keep motor

Thanks! I will keep motor battery separated.