Microcontrollers, Sensors and Other Potions
Physical Computing — September 17, 2020
What I Learned from the Readings
Microcontrollers are the simple parts that interact with the physical world and output the digital result on the simplest levels. Sensors that are used for input and actuators for output might are the part of every microcontroller. There are various parts of microcontrollers as well as programs for the processors like firmware, bootloaders, basic input-output systems and operating systems.
Generally the term microcontrollers refers to firmware-only processor. The processor that runs the IS from external storage is called embedded processor. If it's in a device with lots of other processors like mobile phones, computers etc, it's called central processor.
There are also two types of boards: development board and activity board. Arduino is a development board as usually it includes the processor, power regulation circuity, hardware programmer connector, communications interface circuity and basic indicator LEDs. As for the activity board, it usually contains a pre-programmed microcontroller, some sensors, actuators along with the communicators interface and protocol. So generally activity boards can't operate on their own without being connected to a personal computer.
Sensors
Sensors convert various forms of physical energy into electrical energy. Analog sensors read a variable change. The types of sensors that I have discovered: resistive sensors, optical sensors, ranging sensors and MEMS sensors.
Though, I didn't really understand what the MEMS sensors are used for and how to use them in my own projects. What kind of real life examples are out there?
Testing the Example with the Ultrasonic Ranger
After the readings I decided to try out one of the sensors I had, the ultrasonic ranger. I watched Tom's video in one of the reading's and tried to use the code he was showing there.
There was no direct explanation on what kind of role each pin of the ranger had so the first thing I did was to look up the datasheet. This is how I learned that the ultrasonic ranger has two input pins and two output pins:
- Vcc – powers the sensor, typically is connected to +5V.
- Trigger – input pin.
- Echo - output pin.
- Ground - ground.
~
First Mistake
At first I did not really understand whether it was necessary to connect the trigger to the Arduino, as I thought I only wanted to obtain the data from the sensor –– I am not going to send any signals to it. So this was my first circuit structure and code:
void setup() {
Serial.begin(9600);
}
void loop() {
int sensorValue = analogRead(A0);
Serial.println(sensorValue);
delay(1);
}
Spoiler alert: it did not work. I did not consider the tiny thing that in order for the sensor to actually read you still have to send the signal to it so that it knows it should start obtaining the information from the physical world.
Doing Things (Almost) Right via Analog Input
After connecting the trigger pin I got the following output from the sensor.
I also noticed that for some reason the sensor would output some random numbers despite the fact that I place my hand very close or very far away from the sensor. This is when I started researching the alternatives for my code. I found out that analog input works only for the type of sensors that output the voltage change (variable resistors). For the other types you have to use different function. This was also the reason why the analog input in my case was having weird numbers.
Doing Things Right via PulseIn
While searching for another solution on the internet I found out that you can get a similar result using the pulseIn()
function and predefining the input and output pins in the setup()
. The pulseIn()
function counts how many signals go out from the sensor and then come back to it which is ideal for the ultrasonic ranger. I connected the trigger to the pin 2 and echo to the pin 3.
int distance;
long duration;
void setup() {
pinMode(2, OUTPUT);
pinMode(3, INPUT);
Serial.begin(9600);
Serial.println("started");
}
void loop() {
digitalWrite(2, LOW);
delayMicroseconds(2);
digitalWrite(2, HIGH);
delayMicroseconds(10);
digitalWrite(2, LOW);
duration = pulseIn(3, HIGH);
distance = duration * 0.034 / 2;
Serial.println(distance);
delay(500);
}
The result was also easier to believe in.
The digit in the console would decrease when my hand is close to the sensor and would increase if vice versa.
Ultrasonic Range and LEDs
I decided to go further and experiment with LEDs while using the sensor. So I connected the LEDs with a resistor to the output pin number 10 and set the if/else statement for if the object is too close to the sensor the LED lights up. To do that I added the following code to the existing one:
...
void setup() {
...
pinMode(10, OUTPUT);
...
}
void loop() {
digitalWrite(2, LOW);
digitalWrite(10, LOW);
delayMicroseconds(2);
digitalWrite(2, HIGH);
delayMicroseconds(10);
digitalWrite(2, LOW);
duration = pulseIn(3, HIGH);
distance = duration * 0.034 / 2;
if(distance <= 10) {
digitalWrite(10, HIGH);
delay(500);
}
Serial.println(distance);
delay(500);
}
Isn't it like a social distancing alert device?!
Digital Input with a Switch
I finally set up the breadboard beautifully! Spent almost 5 minutes to cut all the wires to connect the two parts so that it looked cleaner that it used to be.
void setup() {
pinMode(2,INPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
}
void loop() {
if(digitalRead(2) == HIGH) {
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
}
else {
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
}
}
It still takes time to understand how the switch exactly works and why do we have 4 legs for it. It is also important to have the pulldown resistor that connects the switch to the ground. It does work weirdly if you take it out and replace with a wire instead but I would like to understand the theory behind that!
It's also important to note that you should be careful experimenting without the resistor. My Arduino restarted on its own for a few times when it was working without the resistor between the switch and the ground.
Using Analog Input with the Right Sensors
I tried to use the potentiometer with the analog input to lit up a LED. This time it worked perfectly. What I also found out was the value the analog input has is the value we use to regulate the voltage for the other parts in the circuits. For example, in this case we take the value of the analog input to convert it into the value for the LED.
const int ledPin = 2;
int analogValue = 0;
int brightness = 0;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop() {
analogValue = analogRead(A0);
Serial.println(analogValue);
brightness = (analogValue/4) * 10;
analogWrite(ledPin, brightness);
// Serial.println(brightness);
}
If you rotate the potentiometer to the value to high for the LED to handle, there is the resistor that comes into place, which is why you might see the effect where the LED is less bright with the higher analog values.
c: