BrainClip Analog

BrainClip Analog

This lesson covers the Analog Modules.

Prerequisite

The BrainClip Digital lesson must be completed before this lesson.

Analog Input

An analog input is capable of reading a voltage level on a pin, not just on and off. Since computers are only capable of reading a zero and a one, a special circuit is added that converts the voltage level to a number that the computer understands. On the BrainPad, the returned number is 0 when the pin has no voltage and 100 when the pin has maximum voltage, which is 3.3V. Not all pins are analog capable. The pinout of the BrainPad shows what pins are analog capable, but all large pads are analog capable.

Rocker Module

A good example of an analog input is the Rocker Module.

This module consists of two variable resistors, one is connected on the X axis and the other on the Y axis. The connections are as follows:

  • X -> P0
  • Y -> P1
  • GND -> GND
  • VCC -> 3V
var x = Analog(P0);
var y = Analog(P1);

while(true){
    var xValue = In(x);
    var yValue = In(y);
    Print("x = " + xValue);
    Print("y = " + yValue);
}
x = Analog(P0)
y = Analog(P1)

while True:
    xValue = In(x)
    yValue = In(y)
    Print("x = " + str(xValue))
    Print("y = " + str(yValue))
 

The values will show on the screen, but let’s do something more interesting. How about a circle that is positioned on the screen in correspondence to the rocker position?

Tip: Since we’re drawing a Circle, don’t forget to add the using static BrainPad.Display; to the top of our code if you’re programming in C#. When using Python, Display library is built-in so there’s nothing to add.

var x = Analog(P0);
var y = Analog(P1);

while(true){
    Clear();
    var xValue = In(x);
    var yValue = In(y);
    Circle(xValue*128/100, yValue*64/100, 10);
    Show();
}
x = Analog(P0)
y = Analog(P1)

while True:
    Clear()
    xValue = In(x)
    yValue = In(y)
    Circle(xValue*128/100, yValue*64/100, 10)
    Show()
 

Light Sensor Module

Another analog sensor is the light sensor.

The connections are as follows:

  • OUT -> P0
  • GND -> GND
  • VCC -> 3V

Reading the sensor is simple and can be done in a loop.

var light = Analog(P0);

while (true) {
    var lightLevel = In(light);
    Print("light = " + lightLevel);
}
light = Analog(P0)

while True:
    lightLevel = In(light)
    Print("light = " + str(lightLevel))
  

Analog sensors output a voltage and that voltage can mean something. In this case, the voltage level is the reflecting the light level. But what scale or what unit? This is where a function can be added to scale the value from 0-100 level to mean something useful.

Trying the code above with different light levels, we can see that the brighter the environment the lower the number gets. This may not be desired as we want the numbers to go up with the light level. This is easily accomplished by subtracting the value from 100 since the max value is 100.

var light = Analog(P0);

while (true) {
    var lightLevel = In(light);
    lightLevel = 100 - lightLevel;
    Print("light = " + lightLevel);
}
light = Analog(P0)

while True:
    lightLevel = In(light)
    lightLevel = 100 - lightLevel
    Print("light = " + str(lightLevel))
 

But what is dark and what is bright? This is defined in your application. For example, we can say less than level 30 is dark and we need to turn the lights on. We do not have a light, but we can print the results on the screen.

var light = Analog(P0);

while (true) {
    var lightLevel = In(light);
    lightLevel = 100 - lightLevel;
    if (lightLevel < 30) {
        Print("Dark!");
    }
    else {
        Print("Bright!");
    }
}
light = Analog(P0)

while True:
    lightLevel = In(light)
    lightLevel = 100 - lightLevel
    if lightLevel < 30:
        Print("Dark!")
    else:
        Print("Bright!)
 

 

And here is the same code, but this time we will use the onboard LED.

var light = Analog(P0);
var onboardLed = Digital(Led);

while (true) {
    var lightLevel = In(light);
    lightLevel = 100 - lightLevel;
    if (lightLevel < 30) {
        Out(onboardLed, 1);
    }
    else {
        Out(onboardLed, 0);
    }
}
light = Analog(P0)
onboardLed = Digital(Led)

while True:
    lightLevel = In(light)
    lightLevel = 100 - lightLevel
    if lightLevel < 30:
        Out(onboardLed, 1)
    else:
        Out(onboardLed, 0)

 
 

How difficult might it be to draw a graph of light level? We can draw a horizontal line from the bottom (Y = 64) to the “light level”, which was scaled to be anywhere 0 to 64. The line can be somewhere on the screen, so let’s place it at 10.

var light = Analog(P0);

while (true) {
    var lightLevel = In(light);
    lightLevel = lightLevel * 64 / 100;
    Clear();
    Line(10, lightLevel, 10, 64);
    Show();
}
light = Analog(P0)

while True:
    lightLevel = In(light)
    lightLevel = lightLevel * 64 / 100
    Clear()
    Line(10, lightLevel, 10, 64)
    Show()
 

The line can be replaced with a rectangle to make it thicker, but what we want to here is make a graph. We can do that by keeping the line where it is and then draw another line next to it with the new light level. We keep doing this all the way across the screen. That is moving the line on the x axis one in every loop. When we reach the end of the screen, that is x is more than 128, we then clear the screen and start from the beginning.

var light = Analog(P0);
var x = 0;

while (true) {
    var lightLevel = In(light);
    lightLevel = lightLevel * 64 / 100;
    x = x + 1;
    if (x > 128) {
        x = 0;
        Clear();
    }
    Line(x, lightLevel, x, 64);
    Show();
}
light = Analog(P0)
x = 0

while True:
    lightLevel = In(light)
    lightLevel = lightLevel * 64 / 100
    x = x + 1
    if x > 128:
        x = 0
        Clear()
    Line(x, lightLevel, x, 64)
    Show();

 

Did we just make a light-level drawing artist tool?!?

Analog Output

An analog output controls the voltage level produced on a pin. As it turns out, it is very difficult to control an analog value. A better alternative is to toggle a pin on and off very rapidly to reduce the energy produced on that pin. Consider this, a light connected to a pin and that pin is on half the time and off half the time would result in us seeing the light coming on and off. Now what will happen if the light is doing this 1000 times per second. Our eyes can’t see a light blinking this fast and instead we will see the light dimmer. Similarly when controlling a motor, like a fan, where the power is on half the time and off half the time, the motor is receiving half the energy on average.

var level = Analog(P1);
var x = 0;

while (true) {
    x = x + 10;
    if (x > 100) {
        x = 0;
    }
    Out(level, x);
    Print(x);
    Wait(0.1);
}
light = Analog(P0)
x = 0

while True:
    x = x + 10
    if x > 100:
        x = 0
    Out(level, x)
    Print(x)
    Wait(0.1)

 

What’s Next?

The BrainClip Beyond lesson covers the other modules.


BrainStorm

Newsletter

Twitter Feed
Hot off the press!
March 13, 2023
November 9, 2022
September 15, 2022
March 21, 2022
March 21, 2022