Lesson 8--Microduino "Pulse Recorder"

From Microduino Wiki
Jump to: navigation, search
Language: English  • 中文

Objective

In lesson 3, you learned how to use a button and avoid the issue of electrical noise. The solution was to add a short delay. However, this solution has a disadvantage. Different people will take different amounts of time to press a button. That means the delay must be different for different people, too. This lesson will teach you how to get the duration that a button is pressed down. This program uses pulse timing calculation. In addition, you will learn about Arduino's serial port to monitor the data.

Equipment

Lesson8All.jpg

Pulse

Pulse is a rapid, transient change in the amplitude of a signal from a baseline value to a higher or lower value, followed by a rapid return to the baseline value. In Microduino, pulse is simply a digital signal which changes between high and low in a cyclical pattern. Pulse timing is often used to calculate the speed in electronic components.

Experiment Schematic

Lesson8-schematic.jpg

This schematic uses the internal pull-up. We also connected an external ceramic capacitor for stabilization.

Program

int pin = 2;  //Define Pin D2
float time1,time2;  //Define variables to float
 
void setup()
{
  Serial.begin(115200);  //Serial port baud rate
  pinMode(pin,  INPUT_PULLUP); //Set pin to input mode with the internal pull-up
}
 
void loop()
{
    //Read low pulse from the pin, the maximum pulse interval is 60 seconds, and assign the result to the variable time1
  time1= pulseIn(pin, LOW, 60000000)/1000;        //Convert the time to ms
  Serial.print(time1);                            //Output the time1 by serial
  Serial.print("ms  ");
  time2= pulseIn(pin, LOW,60000000)/1000.0;       //Convert the time to ms
  Serial.print(time2);                            //Output time1 by serial
  Serial.println("ms");                           //Output time2 by serial and start a new line
}

Serial Monitoring

Open the serial port monitor

  • Click the Serial Monitor button to pop-up the serial monitor interface.
Lesson8-serialmonitor.jpg
Lesson8-serialwindow.jpg
  • Note:
    • In order to use serial, you must add the line "Serial.begin(xxxx)" in function setup(),xxxx is the baud rate. The baud rate is the rate at which data is transferred to your computer from the Microduino-Core. The units is bits per second.
    • It is important that the baud rate should be set to the same number when using the serial monitor. If they do not match up, you will only see gibberish on your serial monitor.
  • When converting units (as in the case of microseconds to milliseconds), you need to pay attention to the data types:
Lesson-calculate.jpg

pulseIn() usage

  • Function:Reads a pulse (either HIGH or LOW) on a pin. For example, if value is HIGH, pulseIn() waits for the pin to go HIGH, starts timing, then waits for the pin to go LOW and stops timing. Returns the length of the pulse in microseconds. Gives up and returns 0 if no pulse starts within a specified time out. This function is accurate for pulse ranges of 10microseconds ~ 3mins. (1s=1,000 milliseconds=1,000,000 microseconds)
  • Syntax:
    • pulseIn(pin, value)
    • pulseIn(pin, value, timeout)
  • Parameters:
    • pin: the number of the pin on which you want to read the pulse. (int)
    • value: type of pulse to read: either HIGH or LOW. (int)
    • timeout (optional):the number of microseconds to wait for the pulse to start; default is one second (unsigned long)

Result

For stabilization, add a ceramic capacitor in the signal change port. You will be able to see better results.

Lesson8-capacitance.jpg
Lesson8Result.jpg

Video