Python time.sleep_us() Examples

The following are 16 code examples for showing how to use time.sleep_us(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module time , or try the search function .

Example 1
Project: micropython-hcsr04   Author: rsc1975   File: hcsr04.py    License: Apache License 2.0 6 votes vote down vote up
def _send_pulse_and_wait(self):
        """
        Send the pulse to trigger and listen on echo pin.
        We use the method `machine.time_pulse_us()` to get the microseconds until the echo is received.
        """
        self.trigger.value(0) # Stabilize the sensor
        time.sleep_us(5)
        self.trigger.value(1)
        # Send a 10us pulse.
        time.sleep_us(10)
        self.trigger.value(0)
        try:
            pulse_time = machine.time_pulse_us(self.echo, 1, self.echo_timeout_us)
            return pulse_time
        except OSError as ex:
            if ex.args[0] == 110: # 110 = ETIMEDOUT
                raise OSError('Out of range')
            raise ex 
Example 2
Project: developer-badge-2018-apps   Author: IBM-Developer-Korea   File: gp2y1014au.py    License: Apache License 2.0 6 votes vote down vote up
def readVoRaw(self, samplingTime, deltaTime):
        # Turn on ILED
        self.sharpLEDPin.value(0)

        # Wait 0.28ms 
        time.sleep_us(samplingTime)

        VoRaw = self.sharpVoPin.read()

        # Wait 0.04ms
        time.sleep_us(deltaTime)

        # Turn off ILED
        self.sharpLEDPin.value(1)

        return VoRaw 
Example 3
Project: pysmartnode   Author: kevinkk525   File: hcsr04.py    License: MIT License 6 votes vote down vote up
def _pulse(self) -> int:
        """
        Send a pulse and wait for the echo pin using machine.time_pulse_us() to measure us.
        :return: int
        """
        tr = self._tr
        tr.value(0)
        time.sleep_us(5)
        tr.value(1)
        time.sleep_us(10)
        tr.value(0)
        try:
            return machine.time_pulse_us(self._ec, 1, self._to)
        except OSError as e:
            if e.args[0] == 100:  # TIMEOUT
                raise OSError("Object too far")
            raise e 
Example 4
Project: developer-badge-2018-apps   Author: IBM-Developer-Korea   File: dust_firmware_avg.py    License: Apache License 2.0 5 votes vote down vote up
def readValue():
    global Voc, Vos, idx

    VoRaw = dust_sensor.readRawVo(220, 40) # adjusted 280 -> 220

    # Compute the output voltage in Volts.
    Vo = VoRaw / 4095.0 * 5.0

    # Exponential Moving Average
    # https://www.investopedia.com/terms/e/ema.asp
    # EMA(t) = Value(t)*k + EMA(y) * (1-k)
    # k = 2 / (N+1)
    # k = 0.005 where N = 399
    Vos = Vo * 0.005 + Vos * 0.995
        
    # print('{}, {}, {}, {}'.format(idx, VoRaw, Vo*1000.0, Vos*1000.0))

    density = (Vos - 0.6) / 0.5 * 100.0
    if density < 0:
      density = 0

    print('{}, {}, {}'.format(Vo*1000.0, Vos*1000.0, density*10))
    
    status_box.text(density+'ug/m3')

    idx += 1

    #time.sleep_us(10000 - 320)
    time.sleep_us(4000) 
Example 5
Project: developer-badge-2018-apps   Author: IBM-Developer-Korea   File: dust_average.py    License: Apache License 2.0 5 votes vote down vote up
def readValue():
    global Voc, Vos, idx

    # Turn on ILED
    sharpLEDPin.value(0)

    # Wait 0.28 ms 
    time.sleep_us(280)

    VoRaw = sharpVoPin.read()

    # Wait 0.04 ms
    #time.sleep_us(40)

    # Turn off ILED
    sharpLEDPin.value(1)

    # Compute the output voltage in Volts.
    Vo = VoRaw / 4095.0 * 3.3

    # Exponential Moving Average
    # https://www.investopedia.com/terms/e/ema.asp
    # EMA(t) = Value(t)*k + EMA(y) * (1-k)
    # k = 2 / (N+1)
    # k = 0.005 where N = 399
    Vos = Vo * 0.005 + Vos * 0.995
    dV = Vos - Voc
    if dV < 0:
        dV = 0
        Voc = Vo

    # Convert to Dust Density in units of ug/m3.
    dustDensity = dV / K * 100.0
    
    print('VoRaw={}, Vo={} mV, Density={} ug/m3'.format(VoRaw, Vo*1000.0, dustDensity))

    idx += 1

    time.sleep_us(10000 - 320) 
Example 6
Project: developer-badge-2018-apps   Author: IBM-Developer-Korea   File: dust_firmware.py    License: Apache License 2.0 5 votes vote down vote up
def readValue():
    global Voc

    # Turn on ILED
    sharpLEDPin.value(0)

    # Wait 0.28 ms 
    # time.sleep_us(280) # Actually, it took 0.4 ms.
    time.sleep_us(196) # It was measured about value of 0.28 ms

    VoRaw = sharpVoPin.read()

    # Wait 0.04 ms
    #time.sleep_us(40)

    # Turn off ILED
    sharpLEDPin.value(1)

    # Compute the output voltage in Volts.
    Vo = VoRaw / 4095.0 * 5.0

    # Convert to Dust Density in units of ug/m3.
    dV = Vo - Voc
    if dV < 0:
        dV = 0
        Voc = Vo
    
    dustDensity = dV / K * 100.0
    
    print('VoRaw={}, Vo={} mV, Density={} ug/m3'.format(VoRaw, Vo*1000.0, dustDensity))

    #time.sleep_us(10000 - 320)
    time.sleep_us(4000) 
Example 7
Project: developer-badge-2018-apps   Author: IBM-Developer-Korea   File: dust.py    License: Apache License 2.0 5 votes vote down vote up
def readValue():
    global Voc

    # Turn on ILED
    sharpLEDPin.value(0)

    # Wait 0.28 ms 
    time.sleep_us(280)

    VoRaw = sharpVoPin.read()

    # Wait 0.04 ms
    #time.sleep_us(40)

    # Turn off ILED
    sharpLEDPin.value(1)

    # Compute the output voltage in Volts.
    Vo = VoRaw / 4095.0 * 3.3

    # Convert to Dust Density in units of ug/m3.
    dV = Vo - Voc
    if dV < 0:
        dV = 0
        Voc = Vo

    # Convert to Dust Density in units of ug/m3.
    dustDensity = dV / K * 100.0
    
    print('VoRaw={}, Vo={} mV, Density={} ug/m3'.format(VoRaw, Vo*1000.0, dustDensity))

    time.sleep_us(10000 - 320) 
Example 8
Project: 1ZLAB_PyEspCar   Author: 1zlab   File: pca9685.py    License: GNU General Public License v3.0 5 votes vote down vote up
def freq(self, freq=None):
        if freq is None:
            return int(25000000.0 / 4096 / (self._read(0xfe) - 0.5))
        prescale = int(25000000.0 / 4096.0 / freq + 0.5)
        old_mode = self._read(0x00) # Mode 1
        self._write(0x00, (old_mode & 0x7F) | 0x10) # Mode 1, sleep
        self._write(0xfe, prescale) # Prescale
        self._write(0x00, old_mode) # Mode 1
        time.sleep_us(5)
        self._write(0x00, old_mode | 0xa1) # Mode 1, autoincrement on 
Example 9
Project: Smart-IoT-Planting-System   Author: Python-IoT   File: upcd8544.py    License: MIT License 5 votes vote down vote up
def sleep_us(self, useconds):
        try:
            time.sleep_us(useconds)
        except AttributeError:
            machine.udelay(useconds) 
Example 10
Project: Smart-IoT-Planting-System   Author: Python-IoT   File: upcd8544.py    License: MIT License 5 votes vote down vote up
def reset(self):
        """ issue reset impulse to reset the display """
        self.rst.value(0)  # RST on
        self.sleep_us(100) # reset impulse has to be >100 ns and <100 ms
        self.rst.value(1)  # RST off
        # Defaults after reset:
        self.power      = self.POWER_DOWN
        self.addressing = self.ADDRESSING_HORIZ
        self.instr      = self.INSTR_BASIC
        self.display_mode = self.DISPLAY_BLANK
        self.temp_coeff = self.TEMP_COEFF_0
        self.bias       = self.BIAS_1_11
        self.voltage    = 3060 
Example 11
Project: uPySensors   Author: lemariva   File: hcsr04.py    License: Apache License 2.0 5 votes vote down vote up
def _send_pulse_and_wait(self):
        """
        Send the pulse to trigger and listen on echo pin.
        We use the method `machine.time_pulse_us()` to get the microseconds until the echo is received.
        """
        self.trigger.value(0) # Stabilize the sensor
        time.sleep_us(5)
        self.trigger.value(1)
        # Send a 10us pulse.
        time.sleep_us(10)
        self.trigger.value(0)
        try:
            if (uname().sysname == 'WiPy'):
                pulse_list = pulses_get(self.echo, self.echo_timeout_us)
                if(len(pulse_list) == 0):
                    pulse_time = -1
                else:
                    pulse_time = pulse_list[0][1]
            else:
                pulse_time = time_pulse_us(self.echo, 1, self.echo_timeout_us)

            return pulse_time
        except OSError as ex:
            if ex.args[0] == 110: # 110 = ETIMEDOUT
                raise OSError('Out of range')
            raise ex 
Example 12
Project: uPySensors   Author: lemariva   File: ST7735.py    License: Apache License 2.0 5 votes vote down vote up
def _reset( self ) :
    '''Reset the device.'''
    self.dc(0)
    self.reset(1)
    time.sleep_us(500)
    self.reset(0)
    time.sleep_us(500)
    self.reset(1)
    time.sleep_us(500) 
Example 13
Project: MaixPy_scripts   Author: sipeed   File: RGB_LED.py    License: MIT License 5 votes vote down vote up
def write_clk(self):
        self.clk.value(0)
        time.sleep_us(20)
        self.clk.value(1)
        time.sleep_us(20) 
Example 14
Project: 1ZLAB_Face_Track_Robot   Author: 1zlab   File: pca9685.py    License: GNU General Public License v3.0 5 votes vote down vote up
def freq(self, freq=None):
        if freq is None:
            return int(25000000.0 / 4096 / (self._read(0xfe) - 0.5))
        prescale = int(25000000.0 / 4096.0 / freq + 0.5)
        old_mode = self._read(0x00) # Mode 1
        self._write(0x00, (old_mode & 0x7F) | 0x10) # Mode 1, sleep
        self._write(0xfe, prescale) # Prescale
        self._write(0x00, old_mode) # Mode 1
        time.sleep_us(5)
        self._write(0x00, old_mode | 0xa1) # Mode 1, autoincrement on 
Example 15
Project: micropython-adafruit-pca9685   Author: adafruit   File: pca9685.py    License: MIT License 5 votes vote down vote up
def freq(self, freq=None):
        if freq is None:
            return int(25000000.0 / 4096 / (self._read(0xfe) - 0.5))
        prescale = int(25000000.0 / 4096.0 / freq + 0.5)
        old_mode = self._read(0x00) # Mode 1
        self._write(0x00, (old_mode & 0x7F) | 0x10) # Mode 1, sleep
        self._write(0xfe, prescale) # Prescale
        self._write(0x00, old_mode) # Mode 1
        time.sleep_us(5)
        self._write(0x00, old_mode | 0xa1) # Mode 1, autoincrement on 
Example 16
Project: developer-badge-2018-apps   Author: IBM-Developer-Korea   File: dust_average copy.py    License: Apache License 2.0 4 votes vote down vote up
def readValue():
    global Voc, Vos, idx

    # Turn on ILED
    sharpLEDPin.value(0)

    # Wait 0.28 ms 
    # time.sleep_us(280) # Actually, it took 0.4 ms.
    time.sleep_us(196) # It was measured about value of 0.28 ms

    VoRaw = sharpVoPin.read()

    # Wait 0.04 ms
    #time.sleep_us(40)

    # Turn off ILED
    sharpLEDPin.value(1)

    # Compute the output voltage in Volts.
    Vo = VoRaw / 4095.0 * 3.3

    # Exponential Moving Average
    # https://www.investopedia.com/terms/e/ema.asp
    # EMA(t) = Value(t)*k + EMA(y) * (1-k)
    # k = 2 / (N+1)
    # k = 0.005 where N = 399
    Vos = Vo * 0.005 + Vos * 0.995
    dV = Vos - Voc
    if dV < 0:
        dV = 0

    # Convert to Dust Density in units of ug/m3.
    dustDensity = dV / K * 100.0
    
    #print('VoRaw={}, Vo={} mV, Density={} ug/m3'.format(VoRaw, Vo*1000.0, dustDensity))
    
    print('{}, {}, {}'.format(idx, Vo*1000.0, Vos*1000.0))

    status_box.text(str(dustDensity)+'ug/m3')

    idx += 1

    #time.sleep_us(10000 - 320)
    time.sleep_us(4000)