And this brings me to my question again: how do you integrate other sensors with the main processors.
- You read the datasheet of the sensor.
- You learn what you have to do in order to make the sensor work.
- You program the microcontroller to do that.
For example:
The sensor has a clock wire and a data wire (or "line" or "pin"). The datasheet says it reads an 8-bit value, and outputs the next bit on the data wire every time the clock wire goes high. The most-significant bit (MSB) is first. After 8 bits are read, the sensor takes the next reading.
To get the value from the sensor, you would need to make the clock wire high (and then low again) 8 times, and check whether the data wire is high or low in between each pulse.
You write the code:
result = 0;
for(int k = 0; k < 8; k++) {
output(SENSOR_CLOCK, 1);
output(SENSOR_CLOCK, 0);
if(input(SENSOR_DATA))
result |= 1 << k;
}
You test it and realize your sensor readings are nonsense. You realize your code reads the LSB first instead of the MSB first, so you change it to result |= 1 << (7 - k)
and it works. Just like any other program, don't expect to get it right the first time!
Now, you will often find that your microcontroller can't do the thing you need it to do. A good example is analog sensors, which don't just output high or low, but any voltage in between. Some microcontrollers have analog pins which can read analog values. Others don't. And the ones which do have them have a limited amount, and you might need more sensors than that, and they have limited precision, and you might need a more accurate measurement. So you research and buy an analog-to-digital converter (ADC). Now you connect the sensor to the analog side of the ADC, and you connect the digital side to the microcontroller, and you follow steps 1-4 according to the datasheet of the ADC, because the ADC is acting as a "voltage sensor". And this lets your microcontroller read the voltage going into the ADC, which tells it the original thing you were trying to sense.
Or, you might have a digital sensor, but it uses a different voltage than the microcontroller. In this case you'll need to build a little circuit to translate the voltage, so that when your microcontroller outputs 3.3V (high), the sensor receives 1.8V (its version of high), and when your microcontroller outputs 0V, the sensor receives 0V.
You might have an analog sensor where the voltage only changes a tiny bit, and you might want to build an analog amplifier to make the change bigger before you convert it to digital.
These are some of the problems you may run into when connecting a sensor to a microcontroller. The exact problems you'll run into depend on the type of sensor. They are all different.
I have no idea about any of those technical stuff (like I2C, SPI, UART, Clock, PWM, etc.)
Then you learn as you go. I2C, SPI, and UART are all digital protocols that a lot of chips use to talk to other chips. A protocol is a way that communication happens, like the "clock and data protocol" I used as an example at the beginning. They tell you how to communicate.
SPI and I2C are pretty similar to the "clock and data protocol". UART is quite different, it doesn't have a clock and you read the bits based on a time delay. PWM is a way to emulate an analog output signal, so instead of outputting let's say 3.5V, you output 5V 70% of the time and 0V 30% of the time, and it averages out to 3.5V.
Note that every protocol has two sides - one chip is in control, and the other chip is responding to it. As the microcontroller, most of the time you're the controlling side. You are telling everything else what to do. But sometimes you're the responding side, like if your gadget connects to a computer, and the computer tells your gadget what to do. In this case, it's good to remember that you can read the protocol "in reverse". If the protocol says the controlling side makes the clock high and then low and then inputs a bit, you'd better figure out that you need to output a bit before the clock goes low (i.e. after it goes high) so that the controlling device can read it.
It's not like I have never heard about them, but I am sure I don't know enough to start building an embedded system.
Then learn as you go. "Okay, this sensor needs an I2C connection. What does that actually mean?" The datasheet shouldn't just say "I2C", it should also include timing diagrams showing you exactly which wires have to go high and low in which order. Great! Implement that diagram, and you've implemented I2C.
After reading a few blogs on Microcontrollers, I know datasheets are used to figure out the value of the resistor, capacitor, and other things
This falls into the analog stuff I was talking about above. You don't know, in general. It depends on the specific sensor. Often you don't need any extra resistors or capacitors.
Also this wouldn't be specific to microcontrollers. If you're designing analog circuitry, it's the same skillset, no matter whether you're doing it for a microcontroller to read a sensor, or for something completely different. If you want to smooth out a PWM signal with a resistor/capacitor (RC filter) it's the same as smoothing out any other signal with an RC filter. Don't forget you can simulate these things on your computer! CircuitJS (a.k.a. "falstad") is a popular simulator for beginners due to its ease of use and interactive nature, though note that it doesn't simulate more advanced concepts like parasitic components.