Recitation 3: Sensors by Liyang Zhu (Tom)

In this week’s recitation, we tested some sensors.

Moisture Sensor

First, I followed this form and connected sensor to Arduino.

Arduino Port Sensor Port
5V Red
GND Black
White
A0 Yellow

Then I found a sample code on http://wiki.seeedstudio.com/Grove-Moisture_Sensor/.

int sensorPin = A0;
int sensorValue = 0;

void setup() {
    Serial.begin(9600);
}
void loop() {
    // read the value from the sensor:
    sensorValue = analogRead(sensorPin);
    Serial.print("Moisture = " );
    Serial.println(sensorValue);
    delay(1000);
}

Upload. Done! Here are the photo and video.

Infrared Distance Sensor

First I followed the circuit diagram.

Picture of The Circuit

Image from https://www.instructables.com/id/How-to-Use-the-Sharp-IR-Sensor-GP2Y0A41SK0F-Arduin/

Then, I uploaded the code.

#define sensor A0 // Sharp IR GP2Y0A41SK0F (4-30cm, analog)

void setup() {
Serial.begin(9600); // start the serial port
}

void loop() {

// 5v
float volts = analogRead(sensor)*0.0048828125; // value from sensor * (5/1024)
int distance = 13*pow(volts, -1); // worked out from datasheet graph
delay(1000); // slow down serial port

if (distance <= 30){
Serial.println(distance); // print the distance
}
}

Here is what I’ve done.

Vibration Sensor

I first connect everything.

Image from https://www.arduino.cc/en/Tutorial/Knock

Then I loaded the code from Arduino IDE’s sample library.

Here is my work.

Ultrasonic Ranger

I connected the sensor to Arduino like this.

Arduino Port Sensor Port
4 Echo
5 Trig
5V VCC
GND GND

Then I used the code provided online.

int inputPin=4;
int outputPin=5;
int ledpin=13;
void setup()
{
Serial.begin(9600);
pinMode(ledpin,OUTPUT);
pinMode(inputPin, INPUT);
pinMode(outputPin, OUTPUT);
}
void loop()

{
digitalWrite(outputPin, LOW);
delayMicroseconds(2);
digitalWrite(outputPin, HIGH);
delayMicroseconds(10);
digitalWrite(outputPin, LOW);
int distance = pulseIn(inputPin, HIGH);
distance= distance/58;
Serial.println(distance); 
delay(50); 
if (distance >=50)
{
digitalWrite(ledpin,HIGH);
}
else
digitalWrite(ledpin,LOW);
}

My result:

Joystick Module

When dealing with this sensor, I had a problem with the internal button.

First, connect the module.

Image from https://www.brainy-bits.com/arduino-joystick-tutorial/

Since basically, this module is two potentiometers and one button, I tried to write the code on my own. Here is my initial code.

int X=A0;
int Y=A1;
int BUTTON=3;
void setup(void)
{
  Serial.begin(9600);
  pinMode(BUTTON,INPUT);
}

void loop(void)
{
  Serial.print("X=");
  Serial.print(analogRead(X));
  Serial.print(",");

  Serial.print("Y=");
  Serial.print(analogRead(Y));
  Serial.print(",");

  Serial.print("BUTTON state = ");
  Serial.println(digitalRead(BUTTON));

  delay(100);
}

However, as uploaded and ran the code, I found that though the X- and Y-axis worked well, the state of button stayed 0.

Later as I checked online code, I found that it was because I forgot to use the internal pull-up resistor.

To make the button working, I should either use INPUT_PULLUP as the parameter or explicitly use digitalWrite(BUTTON,HIGH); to enable internal pull-up resistor after assigning the pin as an input;

or, I build the external circuit.

Questions

Question 1

One of the sensors I intended to assemble was the ultrasonic distance sensor. For the pragmatic purpose, this sensor can be used on automatic robots. Such sensors can help robots to avoid collisions. When a robot is moving forward, the distance sensor helps it to make sure there is no obstacle ahead. If the sensors report a short distance, that means something is in front of the robot, and the robot can stop moving before severe collisions.

Question 2

From my perspective, I think the code is a bridge between human and machine. The code is what computers/microcontrollers follow strictly and what human writes to convey their purpose.  It can be both understood by human and machine. When a human wants computers to do something for them, he/she has to use code to let the computer do exactly what he/she wants.

Question 3

I think human will become lazier due to computers. Nowadays computers can help us to do lots of things. And in the future, computers will only be more powerful. More and more tasks of human will be done by computers. So computers will influence human to behave lazier.

Leave a Reply