3. Basic Examples

3.1. AD1

12-bit analog-to-digital converter.

3.2. HB3

2A H-bridge circuit for DC motor drive up to 12V.

3.2.1. Spin motor

This example:

  1. Spins the motor forwards for 20 seconds
  2. Commands the motor to stop and pauses for 2 seconds
  3. Spins the motor in reverse for 20 seconds
  4. Commands the motor to stop and pauses for 2 seconds
  5. Ramps up the speed across 100 steps in the forward direction
  6. Ramps down the speed across 100 steps in the forward direction
  7. Ramps up speed across 100 steps in the reverse direction
  8. Ramps down the speed across 100 steps in the reverse direction
  9. Loops back to (1)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2017 RS Components Ltd
# SPDX-License-Identifier: MIT License

"""
Spin motor forwards then backwards.
Ramp speed up and then down in forward direction.
Ramp speed up and then down in reverse direction.
"""

from DesignSpark.Pmod.HAT import createPmod
import time

if __name__ == '__main__':

    motor = createPmod('HB3','JAA')

    try:
        while True:
        
            print('fwd')
            motor.forward(20)
            time.sleep(2)
            motor.stop()
            time.sleep(2)
            print('rev')
            motor.reverse(20)
            time.sleep(1)
            motor.stop()
            time.sleep(2)
            
            
            print ('ramp up fwd')
            for i in range(100):
                motor.forward(i)
                time.sleep(.1)
            
            print ('ramp down fwd')
            for i in range(100):
                motor.forward(100-i)
                time.sleep(.1)
                
            motor.stop()
            time.sleep(2)
            
            print ('ramp up rev')
            for i in range(100):
                motor.reverse(i)
                time.sleep(.1)
            
            print ('ramp down rev')
            for i in range(100):
                motor.reverse(100-i)
                time.sleep(.1)
        
    except KeyboardInterrupt:
        pass
    
    finally:
        motor.cleanup() 
    
    
    

Requirements

  • PmodHB3 module connected to port JAA
  • DC motor
  • Motor power supply

3.3. ISNS20

±20A DC or AC input, high accuracy current sensor.

3.4. MIC3

Knowles Acoustics SPA2410LR5H-B MEMs microphone and Texas Instrument’s ADCS7476 12-bit Analog-to-Digital Converter.

3.4.1. Display mic level

Print a continous sound level reading out to the terminal.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2017 RS Components Ltd
# SPDX-License-Identifier: MIT License

"""
Print a continous sound level reading out.
"""

from DesignSpark.Pmod.HAT import createPmod
import time

s = ':'
lut = [s]
for i in range(128):
    s+=':'
    lut.append(s)

if __name__ == '__main__':
    
    mic = createPmod('MIC3','JBA')
    time.sleep(0.1)
    
    try:
        while True:
            int = mic.readIntegerValue()
            #print(int)
            print(lut[int>>5])
            snd = mic.readPhysicalValue()
            #print(snd)
            
            #time.sleep(0.01)
    except KeyboardInterrupt:
        pass
    finally:
        mic.cleanup()

Requirements

  • PmodMIC3 module connected to port JBA

3.5. OLEDrgb

Organic RGB LED module with a 96×64 pixel display capable of 16-bit color resolution.

3.5.1. Display text in a bounding box

Display the text “Hello, World!” in a bounding box.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2017 RS Components Ltd
# SPDX-License-Identifier: MIT License

"""
Display Hello, World! in bounding box.
"""

from DesignSpark.Pmod.HAT import createPmod
from luma.core.render import canvas
from luma.oled.device import ssd1331

if __name__ == '__main__':
    try:
        oled = createPmod('OLEDrgb','JA')
        device = oled.getDevice()
        
        with canvas(device) as draw:
            draw.rectangle(device.bounding_box, outline="white", fill="black")
            draw.text((16,20), "Hello, World!", fill="white")
    
        while True:
            pass
    except KeyboardInterrupt:
        pass
    finally:
        oled.cleanup()

Luma.Core & Luma.OLED API: luma.core.render.canvas, luma.oled.device.ssd1331.

Requirements

  • PmodOLEDrgb connected to port JA

3.6. PmodTC1

A cold-junction thermocouple-to-digital converter module designed for a classic K-Type thermocouple wire. Features a temperature range of -73°C to 482°C with provided wire.

3.7. PmodACL2

A 3-axis MEMS accelerometer module. Features the Analog Devices ADXL362 device with Measurement ranges ±2g, ±4g, ±8g. Up to 12-bit resolution on each axis.

3.8. PmodGPS

A GlobalTop FGPMMOPA6H GPS antenna module to receive position data from GPS satellites.

3.9. PmodSWT

Four slides switches for up to 16x different binary logic inputs.

3.10. PmodLS1

A line follower robot interface system board.

3.11. PmodKYPD

A 16-button keypad.