WEEK13-INPUT DEVICES

This week assessment is measure something, add a sensor to a microcontroller board that you have designed and read it.

Input devices mean get signal from environment. The most simple input device like switch, and some complicated sensor like motion sensor it can get the info of motion like angle speed , distance sensor, magnetic sensor, temperature sensor, light sensor, sound sensor, vibration sensor, force sensor, image sensor. They can get many kinds of info of temperature sound light etc.. I will use a distance sensor get the distance. The distance can get by a sona or laser measuring sensor. We have ultrasonic sensor in the fab, this week I add a sona on my board for it will be used in my final project.

The HC-SR04 ultrasonic sensor uses sonar to determine distance to an object like bats or dolphins do. It offers excellent non-contact range detection with high accuracy and stable readings in an easy-to-use package. From 2cm to 400 cm or 1” to 13 feet. It operation is not affected by sunlight or black material like Sharp rangefinders are (although acoustically soft materials like cloth can be difficult to detect). It comes complete with ultrasonic transmitter and receiver module.

800x600

Features of HC-SR04
• Power Supply :+5V DC
• Quiescent Current : <2mA
• Working Current: 15mA
• Effectual Angle: <15°
• Ranging Distance : 2cm – 400 cm/1" - 13ft
• Resolution : 0.3 cm
• Measuring Angle: 30 degree
• Trigger Input Pulse width: 10uS
• Dimension: 45mm x 20mm x 15mm

800x600
1. Electronic board design and produce.

Open the EAGLE and design it

800x600

The board was designed like this.

800x600

The board was produced.

800x600
Board Download
Trace Download
2.Soldering

Here are the components
MCU-ATtiny44 X1
Capacitor 1μF X2
Resistor 10k X1
Crystal 20MHz X1
Regulator 3.3V X1
Sona HC-SR04 X1
BLE HC05 X1

800x600

It is not easy to solder BLE and sona.

800x600

I do some changes, I solder pinheads on board then sona and BLE can insert it.

800x600

It looks much better.

800x600
3. Programming
#define TrigPin A4
#define EchoPin A5

float getDistance(void)
{
  float distance;
// 产生一个10us的高脉冲去触发TrigPin
  digitalWrite(TrigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(TrigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(TrigPin, LOW);
// 检测脉冲宽度,并计算出距离
  distance = pulseIn(EchoPin, HIGH) / 58.00;
  return distance;
}

void setup()
{
  // 初始化串口通信及连接SR04的引脚
  Serial.begin(9600);
  pinMode(TrigPin, OUTPUT);
  pinMode(EchoPin, INPUT);
}

float dis = 0;
void loop()
{
  dis = getDistance() * 10; //mm
  Serial.println(dis);
  delay(300);
}