10. Input devices


  • individual assignment:
    • measure something: add a sensor to a microcontroller board that you have designed and read it
  • group assignment:
    • probe an input device’s analog levels and digital signals

Ultrasonic Ranger test on my board

There are 6 PINS on my MCU’s ISP pinheader: VCC, GND, SCK, MISO, MOSI, RST. I can use VCC, GND, and SCK/MISO/MOSI (ecah as a Arduino pin) to test a ultrasonic ranger.




#define ECHOPIN 4                            // Pin to receive echo pulse
#define TRIGPIN 5                            // Pin to send trigger pulse

#include <SoftwareSerial.h>
SoftwareSerial mySerial(0, 1); // RX, TX

void setup(){
  pinMode(ECHOPIN, INPUT);

void loop(){
  digitalWrite(TRIGPIN, LOW);                   // Set the trigger pin to low for 2uS
  digitalWrite(TRIGPIN, HIGH);                  // Send a 10uS high to trigger ranging
  digitalWrite(TRIGPIN, LOW);                   // Send pin low again
  int distance = pulseIn(ECHOPIN, HIGH);        // Read in times pulse
  distance= distance/58;                        // Calculate distance from time of pulse
  delay(50);                                    // Wait 50mS before next ranging



The ATtiny44A has no serial function so it can’t upload the program through FTDI even after burning the bootloader.

Although the test was succeed, but I must upload the program through FabISP, and removed the FabISP to connect the sensor every time. It was too complicated for the other test, so I made a Arduino on breadboard to make the sensor test more convenient.

Make a Arduino on breadboard

First, I made a schematic on TinkerCAD:

Here is the boomlist:

  • 1× breadboard
  • 2× LEDs
  • 2× 220 Ohm resistors
  • 1× 10k Ohm resistor
  • 2× 10 uF capacitors
  • 16× MHz clock crystal
  • 2× 22 pF capacitors


Use FabISP to burn the bootloader:


The ATmega328P can use FTDI to upload program after burning the bootloader.

After burning the bootloader. I can use FTDI to upload program(at least now I think so):

But I found a problem, the program is always uploading:

Then I realized that the TX and RX wire should be reversed. Because one device Transfer(TX) signal, the other device Receive(RX) signal.

After that I made a Blink test:

Everthing looks fine. I can use it for the input device test now.

Know about the Sensors

There are two kinds of sensors: the Analog and the Digital(the difference). For the sensors I used, there is a little mark on the corner of it:

The wire of sensor module: most sensor has 3 wire: VCC, GND, SIGNAL. Some signal are Analogs some are digitals. But the wire is the same.

Digital Sensors:

Touch sensor:


int ledPin = 13;                // Connect LED on pin 13, or use the onboard one
int KEY = 2;                 // Connect Touch sensor on Digital Pin 2

void setup(){
  pinMode(ledPin, OUTPUT);      // Set ledPin to output mode
  pinMode(KEY, INPUT);       //Set touch sensor pin to input mode

void loop(){
   if(digitalRead(KEY)==HIGH){       //Read Touch sensor signal
        digitalWrite(ledPin, HIGH);   // if Touch sensor is HIGH, then turn on
        digitalWrite(ledPin, LOW);    // if Touch sensor is LOW, then turn off the led


Analog Sensors:

Ambient light Sensor:


void setup()
  Serial.begin(9600); // open serial port, set the baud rate to 9600 bps
void loop()
      int val=analogRead(0);   //connect grayscale sensor to Analog 0
      Serial.println(val);//print the value to serial        


Rotation Sensor:


const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to
const int analogOutPin = 11; // Analog output pin that the LED is attached to

int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // value output to the PWM (analog out)

void setup() {

void loop() {
  sensorValue = analogRead(analogInPin);
  outputValue = map(sensorValue, 0, 1023, 0, 255);         // map it to the range of the analog out:
  analogWrite(analogOutPin, outputValue);                  // change the analog out value:

  Serial.print("sensor = ");
  Serial.print("\t output = ");



map()Re-maps a number from one range to another. That is, a value of fromLow would get mapped to toLow, a value of fromHigh to toHigh, values in-between to values in-between, etc.


Making my own Arduino for futher test:

Till now I realized that although the Arduino on breadboard can be made very easily but if I add a lot of input and output devices on it, it could be a mess. So I started to design my on board and give it a name. The board will use ATmege328P-AU instead of ATmege328P-PU to make it compact.

(Seen from Making the PCino

Original files:

Ultrasonic Sensor
Touch Sensor
Ambient light Sensor Rotation Sensor