Simple test

Ensure your device works with this simple test.

examples/qwiictwist_simpletest.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#  This is example is for the SparkFun Qwiic Twist RGB Rotary Encoder.
#  SparkFun sells these at its website: www.sparkfun.com
#  Do you like this library? Help support SparkFun. Buy a board!
#  https://www.sparkfun.com/products/15083

"""
 Qwiic Twist Simple Test - qwiictwist_simpletest.py
 Written by Gaston Williams, June 19th, 2019
 The Qwiic Twist is a I2C controlled RGB Rotary Encoder

 Simple Test:
 This program uses the Qwiic Twist CircuitPython Library to change
 that status of the Qwiic Twist Rotary Encoder.
"""

from time import sleep
import board
import busio
import sparkfun_qwiictwist

# Create bus object using our board's I2C port
i2c = busio.I2C(board.SCL, board.SDA)

# Create joystick object
twist = sparkfun_qwiictwist.Sparkfun_QwiicTwist(i2c)

# Check if connected
if twist.connected:
    print('Twist connected.')
else:
    print('Twist does not appear to be connected. Please check wiring.')
    exit()

# Print firmware version and current status
print('Firmware version ' + twist.version)

# Turn the relay on and off
print('Press Ctrl-C to exit program')

try:
    while True:
        print('Count: ' + str(twist.count))
        if twist.pressed:
            print('Pressed!')
        sleep(0.5)

except KeyboardInterrupt:
    pass

Examples

  1. Basic Readings - Print number of steps knob has been twisted.
examples/example1_basic_readings.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#  This is example is for the SparkFun Qwiic Single Twist.
#  SparkFun sells these at its website: www.sparkfun.com
#  Do you like this library? Help support SparkFun. Buy a board!
#  https://www.sparkfun.com/products/15083

"""
 Qwiic Twist Example 1 - example1_basic_readings.py
 Written by Gaston Williams, June 19th, 2019
 Based on Arduino code written by
 Nathan Seidle @ Sparkfun, December 3rd, 2018
 The Qwiic Twist is an I2C controlled RGB Rotary Encoder produced by sparkfun

 Example 1 - Basic Readings:
 This program uses the Qwiic Twist CircuitPython Library to
 control the Qwiic Twist RGB Rotrary Encoder over I2C and print
 the number of steps the encoder has been twisted.
"""

from time import sleep
import board
import busio
import sparkfun_qwiictwist

# Create bus object using our board's I2C port
i2c = busio.I2C(board.SCL, board.SDA)

# Create twist object
twist = sparkfun_qwiictwist.Sparkfun_QwiicTwist(i2c)

print('Qwicc Twist Example 1 Basic Readings')

# Check if connected
if twist.connected:
    print('Twist connected.')
else:
    print('Twist does not appear to be connected. Please check wiring.')
    exit()

print('Type Ctrl-C to exit program.')

try:
    while True:
        print('Count: ' + str(twist.count))
        if twist.pressed:
            print('Pressed!')
        sleep(0.5)

except KeyboardInterrupt:
    pass
  1. Set Color - Set the knob color.
examples/example2_set_color.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#  This is example is for the SparkFun Qwiic Single Twist.
#  SparkFun sells these at its website: www.sparkfun.com
#  Do you like this library? Help support SparkFun. Buy a board!
#  https://www.sparkfun.com/products/15083

"""
 Qwiic Twist Example 2 - example2_set_color.py
 Written by Gaston Williams, June 20th, 2019
 Based on Arduino code written by
 Nathan Seidle @ Sparkfun, December 3rd, 2018
 The Qwiic Twist is an I2C controlled RGB Rotary Encoder produced by sparkfun

 Example 2 - Set Color:
 This program uses the Qwiic Twist CircuitPython Library to
 control the Qwiic Twist RGB Rotrary Encoder over I2C to set
 the knob color to pink.  This value is stored in the Qwiic Twist
 and will be loaded after each power-on.
"""

from time import sleep
import board
import busio
import sparkfun_qwiictwist

# Create bus object using our board's I2C port
i2c = busio.I2C(board.SCL, board.SDA)

# Create twist object
twist = sparkfun_qwiictwist.Sparkfun_QwiicTwist(i2c)

print('Qwicc Twist Example 2 Set Color')

# Check if connected
if twist.connected:
    print('Twist connected.')
else:
    print('Twist does not appear to be connected. Please check wiring.')
    exit()

print('Type Ctrl-C to exit program.')

# Turn off any color connections
twist.connect_color(0, 0, 0)

# Set the knob color to pink (r =100, g=10, b=50)
twist.set_color(100, 10, 50)

# Set a flag to toggle color
is_pink = True

try:
    while True:
        print('Count: ' + str(twist.count))
        if twist.pressed:
            print('Pressed!')
            if is_pink:
                # Set the knob color to blue (r =10, g=10, b=100)
                print('Change color to blue.')
                twist.set_color(10, 10, 100)
                is_pink = False
            else:
                # Set the knob color to pink (r =100, g=10, b=50)
                print('Change color to pink.')
                twist.set_color(100, 10, 50)
                is_pink = True

        sleep(0.2)

except KeyboardInterrupt:
    pass
  1. Crazy Color - Set the knob to random colors.
examples/example3_crazy_color.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#  This is example is for the SparkFun Qwiic Single Twist.
#  SparkFun sells these at its website: www.sparkfun.com
#  Do you like this library? Help support SparkFun. Buy a board!
#  https://www.sparkfun.com/products/15083

"""
 Qwiic Twist Example 3 - example3_crazy_color.py
 Written by Gaston Williams, June 20th, 2019
 Based on Arduino code written by
 Nathan Seidle @ Sparkfun, December 3rd, 2018
 The Qwiic Twist is an I2C controlled RGB Rotary Encoder produced by sparkfun

 Example 3 - Crazy Color:
 This program uses the Qwiic Twist CircuitPython Library to
 control the Qwiic Twist RGB Rotrary Encoder over I2C to set
 the knob color to an endless number of random colors.
"""

from time import sleep
import random

import board
import busio
import sparkfun_qwiictwist

# Create bus object using our board's I2C port
i2c = busio.I2C(board.SCL, board.SDA)

# Create twist object
twist = sparkfun_qwiictwist.Sparkfun_QwiicTwist(i2c)

print('Qwicc Twist Example 3 Crazy Color')

# Check if connected
if twist.connected:
    print('Twist connected.')
else:
    print('Twist does not appear to be connected. Please check wiring.')
    exit()

print('Type Ctrl-C to exit program.')

# Turn off any color connections
twist.connect_color(0, 0, 0)

try:
    while True:
        print('Count: ' + str(twist.count))
        if twist.pressed:
            print('Pressed!')

        # Generate a random rgb value
        red = random.randint(0, 256)
        green = random.randint(0, 256)
        blue = random.randint(0, 256)
        twist.set_color(red, green, blue)

        sleep(0.1)

except KeyboardInterrupt:
    pass
  1. Connect Color - Change the knob color as it is twisted.
examples/example4_connect_colors.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#  This is example is for the SparkFun Qwiic Single Twist.
#  SparkFun sells these at its website: www.sparkfun.com
#  Do you like this library? Help support SparkFun. Buy a board!
#  https://www.sparkfun.com/products/15083

"""
 Qwiic Twist Example 4 - example4_connect_colors.py
 Written by Gaston Williams, June 19th, 2019
 Based on Arduino code written by
 Nathan Seidle @ Sparkfun, December 3rd, 2018
 The Qwiic Twist is an I2C controlled RGB Rotary Encoder produced by sparkfun

 Example 4 - Connect Colors:
 This program uses the Qwiic Twist CircuitPython Library to
 control the Qwiic Twist RGB Rotrary Encoder over I2C to make
 the knob change color as the user turns the device.
 We don't have to send a setColor() or setRed() command each time,
 Qwiic Twist can  change its color independently of the master.
 By connecting a -10 value to red, the red LED will go down 10 in
 brightness with each encoder tick. Connecting 10 to blue will
 increase the blue value with each tick.  These values are stored
 in the Qwiic Twist and will be loaded after each power-on.
"""

from time import sleep
import board
import busio
import sparkfun_qwiictwist

# Create bus object using our board's I2C port
i2c = busio.I2C(board.SCL, board.SDA)

# Create twist object
twist = sparkfun_qwiictwist.Sparkfun_QwiicTwist(i2c)

print('Qwicc Twist Example 4 Connect Colors')

# Check if connected
if twist.connected:
    print('Twist connected.')
else:
    print('Twist does not appear to be connected. Please check wiring.')
    exit()

print('Type Ctrl-C to exit program.')

# Set Red and Blue LED brightnesses to half of max.
twist.set_color(128, 0, 128)

#Set the individual color connections

# Red LED will go down 10 in brightness with each encoder tick
twist.red_connection = -10
# Blue LED will go up 10 in brightness with each encoder tick
twist.blue_connection = 10

# Or use the function below to set all color connections at once
# twist.connect_color(-10, 0, 10)

try:
    while True:
        print('Count: ' + str(twist.count))
        if twist.pressed:
            print('Pressed!')
        sleep(0.5)

except KeyboardInterrupt:
    pass
  1. Timestamps - Show timestamps for when knob is twisted or pressed.
examples/example5_timestamps.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#  This is example is for the SparkFun Qwiic Single Twist.
#  SparkFun sells these at its website: www.sparkfun.com
#  Do you like this library? Help support SparkFun. Buy a board!
#  https://www.sparkfun.com/products/15083

"""
 Qwiic Twist Example 5 - example5_timestamps.py
 Written by Gaston Williams, June 21st, 2019
 Based on Arduino code written by
 Nathan Seidle @ Sparkfun, December 3rd, 2018
 The Qwiic Twist is an I2C controlled RGB Rotary Encoder produced by sparkfun

 Example 5 - Timestamps:
 This program uses the Qwiic Twist CircuitPython Library to
 control the Qwiic Twist RGB Rotrary Encoder over I2C to display
 the time between when the user did an action (such as twist
 knob or press button) and when we queried the device. You don't
 need to constantly poll the Qwiic Twist to see if the user has
 twisted the knob or pressed the button. Instead, check every so
 often and when the isMoved or isClick goes true, then read the
 timestamp and you'll know when the user did their thing.
"""

from time import sleep
import board
import busio
import sparkfun_qwiictwist

# Create bus object using our board's I2C port
i2c = busio.I2C(board.SCL, board.SDA)

# Create twist object
twist = sparkfun_qwiictwist.Sparkfun_QwiicTwist(i2c)

print('Qwicc Twist Example 5 Timestamps')

# Check if connected
if twist.connected:
    print('Twist connected.')
else:
    print('Twist does not appear to be connected. Please check wiring.')
    exit()

print('Type Ctrl-C to exit program.')

try:
    while True:
        print('Count: ' + str(twist.count))
        if twist.moved:
            print('Last Twist time: ' + str(twist.time_since_last_movement))
        if twist.clicked:
            print('Last Button time: ' + str(twist.time_since_last_press))
        if twist.pressed:
            print('Pressed!')
        sleep(1)

except KeyboardInterrupt:
    pass
  1. Difference - Show the diffence between encoder counts when knob is twisted.
examples/example6_difference.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#  This is example is for the SparkFun Qwiic Single Twist.
#  SparkFun sells these at its website: www.sparkfun.com
#  Do you like this library? Help support SparkFun. Buy a board!
#  https://www.sparkfun.com/products/15083

"""
 Qwiic Twist Example 6 - example6_difference.py
 Written by Gaston Williams, June 19th, 2019
 Based on Arduino code written by
 Nathan Seidle @ Sparkfun, December 3rd, 2018
 The Qwiic Twist is an I2C controlled RGB Rotary Encoder produced by sparkfun

 Example 6 - Difference:
 This program uses the Qwiic Twist CircuitPython Library to
 control the Qwiic Twist RGB Rotrary Encoder over I2C to display
 the difference since the last reading. This is helpful if you
 don't care what the cumulative value is, just difference.

 Things like volume control, brightness, etc. your system may
 not need to know an absolute value like 417, but instead that
 the user has  moved the encoder 4 ticks since the last reading.
"""

from time import sleep
import board
import busio
import sparkfun_qwiictwist

# Create bus object using our board's I2C port
i2c = busio.I2C(board.SCL, board.SDA)

# Create twist object
twist = sparkfun_qwiictwist.Sparkfun_QwiicTwist(i2c)

print('Qwicc Twist Example 6 Difference')

# Check if connected
if twist.connected:
    print('Twist connected.')
else:
    print('Twist does not appear to be connected. Please check wiring.')
    exit()

print('Type Ctrl-C to exit program.')

try:
    while True:
        print('Count: ' + str(twist.count))
        print('Difference: ' + str(twist.difference))
        sleep(0.250)

except KeyboardInterrupt:
    pass
  1. Set Count - Change the encoder count reported when knob is twisted.
examples/example7_set_count.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#  This is example is for the SparkFun Qwiic Single Twist.
#  SparkFun sells these at its website: www.sparkfun.com
#  Do you like this library? Help support SparkFun. Buy a board!
#  https://www.sparkfun.com/products/15083

"""
 Qwiic Twist Example 7 - example7_set_count.py
 Written by Gaston Williams, June 21st, 2019
 Based on Arduino code written by
 Nathan Seidle @ Sparkfun, December 3rd, 2018
 The Qwiic Twist is an I2C controlled RGB Rotary Encoder produced by sparkfun

 Example 7 - Set Count:
 This program uses the Qwiic Twist CircuitPython Library to
 control the Qwiic Twist RGB Rotrary Encoder over I2C to set
 the encoder count. There are times when it's necessary encoder
 count to a specific value. Useful when the encoder value is mapped directly
 onto a volume setting, FM freq, etc.
"""

from time import sleep
import board
import busio
import sparkfun_qwiictwist

# Create bus object using our board's I2C port
i2c = busio.I2C(board.SCL, board.SDA)

# Create twist object
twist = sparkfun_qwiictwist.Sparkfun_QwiicTwist(i2c)

print('Qwicc Twist Example 7 Set Count')

# Check if connected
if twist.connected:
    print('Twist connected.')
else:
    print('Twist does not appear to be connected. Please check wiring.')
    exit()

# Set initial value to 1000. Not stored to non-volatile memory.
twist.count = 1000

print('Type Ctrl-C to exit program.')

try:
    while True:
        print('Count: ' + str(twist.count))
        print('Difference: ' + str(twist.difference))
        sleep(0.250)

except KeyboardInterrupt:
    pass
  1. Interrupts - Enable button and twist interrupts.
examples/example8_interruptst.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#  This is example is for the SparkFun Qwiic Single Twist.
#  SparkFun sells these at its website: www.sparkfun.com
#  Do you like this library? Help support SparkFun. Buy a board!
#  https://www.sparkfun.com/products/15083

"""
 Qwiic Twist Example 8 - example8_interrupts.py
 Written by Gaston Williams, June 19th, 2019
 Based on Arduino code written by
 Nathan Seidle @ Sparkfun, December 3rd, 2018
 The Qwiic Twist is an I2C controlled RGB Rotary Encoder produced by sparkfun

 Example 8 - Interrupts:
 This program uses the Qwiic Twist CircuitPython Library to
 control the Qwiic Twist RGB Rotrary Encoder over I2C  to enable
 the button and twist interrupts. Once an interrupt is read,
 it is cleared by the library. For this example you will need to
 connect the INT pin on Qwiic to GPIO D6 on the Raspberry Pi.

 The interrupt will not fire until 250ms after the user has stopped
 turning the encoder. This is so the master is not overwhelmed with
  interrupts while the user is still turning the dial.
"""

# from time import sleep
import board
import digitalio
import busio
import sparkfun_qwiictwist

# Create bus object using our board's I2C port
i2c = busio.I2C(board.SCL, board.SDA)

# Set up Interrupt pin on GPIO D6 with a pull-up resistor
twist_interrupt_pin = digitalio.DigitalInOut(board.D6)
twist_interrupt_pin.direction = digitalio.Direction.INPUT
twist_interrupt_pin.pull = digitalio.Pull.UP

# Create twist object
twist = sparkfun_qwiictwist.Sparkfun_QwiicTwist(i2c)

print('Qwicc Twist Example 8 Interrupts')

# Check if connected
if twist.connected:
    print('Twist connected.')
else:
    print('Twist does not appear to be connected. Please check wiring.')
    exit()

# Optional: You can modify the time between when the user has stopped turning
# and when interrupt is raised

# Set twist timeout to 500ms before interrupt assertion
# twist.int_timeout = 500

print('Type Ctrl-C to exit program.')

try:
    while True:
        # When the interrupt goes low
        if not twist_interrupt_pin.value:
            print('Interrupt:')
            if twist.moved:
                print('Count: ' + str(twist.count))
            if twist.pressed:
                print('Pressed!')
            if twist.clicked:
                print('Clicked!')
            twist.clear_interrupts()

except KeyboardInterrupt:
    pass
  1. Change I2C Address - Change the device I2C address.
examples/example9_change_i2c_address.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#  This is example is for the SparkFun Qwiic Twist.
#  SparkFun sells these at its website: www.sparkfun.com
#  Do you like this library? Help support SparkFun. Buy a board!
#  https://www.sparkfun.com/products/15083

"""
  Qwiic Twist Example 9 - example9_change_i2c_address.py
  Written by Gaston Williams, June 13th, 2019
  Based on Arduino code written by
  Nathan Seidle @ SparkFun Electronics, December 3rd
, 2019
  The Qwiic Twist is an I2C controlled RGB Rotary Encoder

  Example 9 - Change I2C Address and Read Firmware Version:
  This program uses the Qwiic Twist CircuitPython Library to change
  the I2C address for the device. You enter in the DEC value (8-119) or

  HEX value (0x08-0x77) for the new Twist address.  The address is
  changed and the firmware version is printed out to validate the connection.
  You can run example10+i2c_scanner.py to validate the address after the change.

  Syntax: python3 change_i2c_address.py [address]
    where address is an optional address value in decimal or hex
    The default value for the address is 63 [0x3F]
"""

import sys
import board
import busio
import sparkfun_qwiictwist

# The default QwiicTwist i2c address is 0x3F (63)
i2c_address = 0x3F

# print('Arguement count: ' , len(sys.argv))
# print('List: ' + str(sys.argv))

# If we were passed an arguement, then use it as the address
if len(sys.argv) > 1:
    try:
        # check to see if hex or decimal arguement
        if '0x' in sys.argv[1]:
            i2c_address = int(sys.argv[1], 16)
        else:
            i2c_address = int(sys.argv[1])
    except ValueError:
        print('Ignoring invalid arguement: ' + str(sys.argv[1]))

# Show the initial address
print('Current i2c address = ' + str(i2c_address)
      + ' [' + hex(i2c_address) + ']')

# Create library object using our Bus I2C port
i2c = busio.I2C(board.SCL, board.SDA)
twist = sparkfun_qwiictwist.Sparkfun_QwiicTwist(i2c, i2c_address)

if twist.connected:
    print('Qwiic Twist Example.')
else:
    # if we can't connecct, something is wrong so just quit
    print('Twist does not appear to be connected. Please check wiring.')
    exit()

print('Address: ' + str(i2c_address) + ' [' + hex(i2c_address) + ']'
      + ' Version: ' + twist.version)

text = input('Enter a new I2C address (as a decimal from 8 to 119 or hex 0x08 to 0x77):')

# check to see if hex or decimal value
if '0x' in text:
    new_address = int(text, 16)
else:
    new_address = int(text)

print('Changing address to ' + str(new_address)
      + ' [' + hex(new_address) + ']')

result = twist.change_address(new_address)

if result:
    print('Address changed to ' + str(new_address)
          + ' [' + hex(new_address) + ']')
    # After the change check the new connection and show firmware version
    if twist.connected:
        print('Connected to Twist after address change.')
        print('Firmware Version: ' + twist.version)
    else:
        print('Error after address change. Cannot connect to Twist.')

else:
    print('Address change failed.')

# good advice whether the address changed worked or not
print('Run example10_i2c_scanner.py to verify the Qwiic Twist address.')
  1. I2C Scanner - Scan the IC2 bus for devices.
examples/example10_i2c_scanner.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#  This is example is for the SparkFun Qwiic Twist.
#  SparkFun sells these at its website: www.sparkfun.com
#  Do you like this library? Help support SparkFun. Buy a board!
#  https://www.sparkfun.com/products/15083

"""
  Qwiic Twist Example 10 - example10_i2c_Scanner.py
  Written by Gaston Williams, June 13th, 2019
  The Qwiic Twist is an I2C controlled RGB Rotary Encoder

  Example 10 - I2C Scanner
  This progam uses CircuitPython BusIO library to find the current
  address of the Qwiic Twist. It uses the I2C Scanner Example from
  https://learn.adafruit.com/circuitpython-basics-i2c-and-spi/i2c-devices

  The factory default address is 0x3F.
"""

import time

import board
import busio

i2c = busio.I2C(board.SCL, board.SDA)

while not i2c.try_lock():
    pass

print('Press Ctrl-C to exit program')

try:
    while True:
        print('I2C addresses found:',
              [hex(device_address) for device_address in i2c.scan()])
        time.sleep(5)
except KeyboardInterrupt:
    pass

finally:
    i2c.unlock()