Terminology


CircuitPython Modules

Analogio Module

  • AnalogIn: Analog signals are variable and they have multiple states. Analog input signals could represent such items that have a range, such as temperature. AnalogIn creates an object and connects the object to the chosen analog pin as an analog input.

  • lightLevel.value: Read the value on the analog pin and return it. The returned value will be between 0 and 65535 inclusive (16-bit). Even if the underlying analog to digital converter (ADC) is lower resolution, the result will be scaled to be 16-bit.

  • https://circuitpython.readthedocs.io/en/latest/shared-bindings/analogio/index.html

Board Module

Digitalio Module

  • DigitalInOut: Creates new DigitalInOut object associated with pin (set pin to digital input/output).

  • Direction: Defines direction of a digital pin.

    • INPUT: Read digital data in.

    • OUTPUT: Write digital data out.

  • A DigitalInOut is used to digitally control I/O pins. Use Direction.OUTPUT to configure a pin for output.

  • Pull: Defines the pull of a digital input pin.

    • UP: When the input line isn’t being driven the pull up can pull the state of the line high so it reads as true.

    • DOWN: When the input line isn’t being driven the pull down can pull the state of the line low so it reads as false.

  • https://circuitpython.readthedocs.io/en/latest/shared-bindings/digitalio/index.html

Pulseio Module

  • PWM: Pulse Width Modulation.

  • pulseio: Your board has pulseio support, which means you can PWM LEDs, control servos, beep piezos, and manage "pulse train" type devices like DHT22 and Infrared.

    • pulseio.PWMOut: Create a PWM object associated with the given pin. This allows you to write PWM signals out on the given pin .

    • duty_cycle: A 16 bit value that dictates how much of one cycle is high (1) versus low (0). 0xffff will always be high, 0 will always be low and 0x7fff will be half high and then half low.

  • https://circuitpython.readthedocs.io/en/latest/shared-bindings/pulseio/index.html

Random Module

  • random.randint(min, max): Return an integer number between min and max.

  • Example: Specifically, in Electronic Die, we define six LED objects corresponding to six digital pins on the board. We create an LED array, and to randomly turn ON/OFF an LED, we use random.randint(0,5). 0 corresponds to the index of the first element/object in the array, and 5 corresponds to the last element in the array. The random integer chosen between 0-5 will be used as x in leds[x].value = True to turn the randomly chosen LED ON.

  • https://circuitpython.readthedocs.io/en/latest/shared-bindings/random/index.html

Simpleio Module

Time Module

  • time.sleep(seconds): Sleep for a given number of seconds.

  • time.monotonic(): To get the time since a board has been on in CircuitPython, the common function is time.monotonic(). It has no arguments and returns the number of seconds since the board was last powered on (after the code started running on it). Keep in mind, the function returns an always increasing value of time with an unknown reference point. Only use it to compare against other values from monotonic().

  • https://circuitpython.readthedocs.io/en/latest/shared-bindings/time/index.html

Serial Console


Electronic Concepts

Digital vs. Analog

  • Digital inputs/outputs and analog inputs/outputs are different. Specifically, digital input signals are used to represent items that only have two states, such as ON (1) or OFF(0). Similarly, digital output signals are used to control items that, again, only have two states. In the above code, the LED values could either be True/False (ON/OFF). Analog signals are variable, they have multiple states. Analog input signals could represent such item as temperature. In the above code, for example, we use AnalogIn to read where the soft potentiometer is pressed. Analog input converts a voltage level into a digital value that can be stored and processed in a computer. Keep in mind that an analog signal can only be read by an analog I/O (input/output), but a digital one can use both analog or digital I/Os. We will match each with its corresponding pin.

  • (FROM ECE 5 LAB 0) Digital pins are either high or low. 0 or 1. Black or white. Light or dark. Nothing in between. They can be used as inputs AND outputs. These are commonly used for tactile or digital sensors, like buttons and switches. Analog inputs are 0 and 1, with everything in between. Instead of just black and white, you can have dark grey, light grey, etc. These ARE ONLY inputs. When you assign a pin number associated with an analog pin you can use the full name A0, for instance, or simply 0. In this case high refers to 5V and low refers to 0V.

PWM (Pulse-width Modulation)

  • Pulse-width Modulation or pulse-duration modulation, is a method of reducing the average power delivered by an electrical signal, by effectively chopping it up into discrete parts. The average value of voltage fed to the load is controlled by turning the switch between supply and load on and off at a fast rate. A simpler way to describe how it works is to say it is a type of digital signal. This means that the signal can only be high (usually 5V) or low (ground -- 0V) at any time. However, we create a range of results because pulse width modulation allows us to vary how much time the signal is high in an analog fashion. A common way we use them here is to control RGB LEDs or to control the direction of a servo.

  • When the signal is high, we call this "on time". To describe the amount of "on time" , we use the concept of duty cycle. Duty cycle is measured in percentage. The percentage duty cycle specifically describes the percentage of time a digital signal is on over an interval or period of time. This period is the inverse of the frequency of the waveform. If a digital signal spends half of the time on and the other half off, we would say the digital signal has a duty cycle of 50% and resembles an ideal square wave. If the percentage is higher than 50%, the digital signal spends more time in the high state than the low state and vice versa if the duty cycle is less than 50%. Take a look at the graph (on the right) that illustrates the example.

  • PWM guide: https://learn.adafruit.com/adafruit-metro-m0-express-designed-for-circuitpython/circuitpython-pwm



Note: 100% duty cycle would be the same as setting the voltage to 5V (high). 0% duty cycle would be the same as setting the voltage to 0V (low) -- grounding the signal.


Components

LED (Light Emitting Diode)

  • LED: A diode is a device that only allows for the flow of electricity to pass in one direction. A LED is a diode that generates a specific wavelength of light when a voltage is applied across its leads.

  • RGB LED: A combination of three LEDs in just one package: red, green and blue; ... you generate different colors by adjusting the brightness of each of the three LEDs of the RGB LED; to adjust the brightness of each LED, you use a PWM signal.

Light Emitting Diode

Resistor

  • Resistor: An electrical component that limits or regulates the flow of electrical current. Resistors are used for many purposes. A few examples include delimit electric current, voltage division, heat generation, matching and loading circuits, control gain, and fix time constants. The colors on the resistors correspond to the amount of resistance they have. For example, the resistors provided in the lab, are coded orange-orange-brown which is 330 Ohms. You can Google Resistor Color Code chart in order to learn more.

resistor

Photoresistor

  • Photoresistor: Also known as a light-dependent resistor (LDR) and photocell, a photoresistor is a resistor with varying resistance that depends on light intensity (the resistance decreases as light intensity increases, while decreasing light intensity will increase resistance). Thus, a photoresistor’s resistance is inversely proportional to the intensity of light directed on it.

photoresistor

Potentiometer

  • Soft potentiometer: A thin and flexible strip that can detect where pressure is being applied. By pressing down on various parts of the strip, you can vary the resistance. You can use this ability to track movement on the soft pot, or simply as a button. Soft potentiometers could be different, so use your hand to test find the range of values you need in your code.

  • Knob potentiometer: A component that varies the resistance based on the position of the knob.

potentiometer

Micro Servo Motor

  • Micro Servo Motor: A closed-loop mechanism that incorporates positional feedback in order to control the rotational or linear speed and position. The motor is controlled with an electric signal, either analog or digital, which determines the amount of movement which represents the final command position for the shaft.

servo motor