Friday, May 22, 2020

ATOM Matrix: Grove Digital and Analog Sensors

The Grove system is a standardized connection system that allows users to avoid breadboards, jumper wires, or soldiering in order to rapidly build prototypes. The connectors all use four wires: GND, VCC, plus two signal wires. These connectors connect a processor (Arduino, Raspberry Pi, ESP32, etc.) to a Grove module. These modules typically address a single function. The information in this section is based off of the Seeedstudio wiki page.

Grove modules come in four types:

  • Digital
  • Analog
  • I2C
  • UART

The functions of the 4 wires for each type of module are as follows:

Type Pin 1 (Yellow) Pin 2 (White) Pin 3 (Red) Pin 4 (Black)
Digital Primary Digital I/O Secondary Digital I/O VCC 5V or 3.3V GND
Analog Primary Analog I/O Secondary Analog I/O VCC 5V or 3.3V GND
I2C SCL SDA VCC 5V or 3.3V GND
UART RX TX VCC 5V or 3.3V GND

Two modules will be used in this post, a Passive IR (PIR) sensor (digital) and a potentiometer (analog). The code for each is trivial and so is presented without comments.

With the exception of the display, all of the code here can be used for other ESP32 or ESP8266 boards with minimal changes.


Digital Grove Sensor: PIR Motion Sensor

This application reads the PIR sensor, and when motion is detected, the display will be red, otherwise it will be green.

 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
/*
 * Grove-Motion-Sensor.ino
 * 
 * By: Mike Klepper
 * Date: 26 April 2020
 * 
 * Demonstrates how to read values from a PIR motion sensor
 * 
 * See post on patriot-geek.blogspot.com
 * for details
 */

#include "M5Atom.h"

int GRB_COLOR_WHITE = 0xffffff;
int GRB_COLOR_BLACK = 0x000000;
int GRB_COLOR_RED = 0x00ff00;
int GRB_COLOR_ORANGE = 0xa5ff00;
int GRB_COLOR_YELLOW = 0xffff00;
int GRB_COLOR_GREEN = 0xff0000;
int GRB_COLOR_BLUE = 0x0000ff;
int GRB_COLOR_PURPLE = 0x008080;

int sensorPin = 32;
int currentValue = 0;

void setup() 
{
  M5.begin(true, false, true);
  delay(20);
  
  pinMode(sensorPin, INPUT);
}

void loop() 
{
  currentValue = digitalRead(sensorPin);
  
  if(currentValue)
  {
    displayColor(GRB_COLOR_RED, 25);
  }
  else
  {
    displayColor(GRB_COLOR_GREEN, 25);
  }

  delay(100);
  M5.update();
}


void displayColor(int activeColor, int brightness)
{
  M5.dis.clear();
  M5.dis.setBrightness(brightness);
  
  for(int i = 0; i < 25; i++)
  {
      M5.dis.drawpix(i, activeColor);
  }
}


Analog Grove Sensor: Potentiometer

Here, the ATOM Matrix reads from a potentiometer and displays the data. The potentiometer returns values in the range 0 to 4095, except that the high value is returned when the knob is turned all the way to the left! We reverse this in lines 43 - 45.

 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
/*
 * Grove-Angle-Sensor.ino
 * 
 * By: Mike Klepper
 * Date: 26 April 2020
 * 
 * Demonstrates how to read values from a Grove potentiometer
 * 
 * See post on patriot-geek.blogspot.com
 * for details
 */

#include "M5Atom.h"

int GRB_COLOR_WHITE = 0xffffff;
int GRB_COLOR_BLACK = 0x000000;
int GRB_COLOR_RED = 0x00ff00;
int GRB_COLOR_ORANGE = 0xa5ff00;
int GRB_COLOR_YELLOW = 0xffff00;
int GRB_COLOR_GREEN = 0xff0000;
int GRB_COLOR_BLUE = 0x0000ff;
int GRB_COLOR_PURPLE = 0x008080;

int activeColor = GRB_COLOR_GREEN;

int sensorPin = 32;

int lastValue = 0;
int currentValue = 0;

void setup() 
{
  M5.begin(true, false, true);
  delay(20);
  
  pinMode(sensorPin, INPUT);
}

void loop() 
{
  currentValue = analogRead(sensorPin);

  int mappedCurrentValue = map(currentValue, 0, 4095, 25, 0);
  int mappedPreviousValue = map(lastValue, 0, 4095, 25, 0);
  int brightness = map(currentValue, 0, 4095, 25, 0);

  if(mappedCurrentValue != mappedPreviousValue)
  {
    displayValue(mappedCurrentValue, brightness, activeColor);
    Serial.print(currentValue);
    Serial.print(" -> ");
    Serial.println(mappedCurrentValue);
    lastValue = currentValue;
  }
  
  delay(50);
  M5.update();
}

void displayValue(int val, int brightness, int desiredColor)
{
  M5.dis.clear();
  M5.dis.setBrightness(brightness);
  
  for(int i = 0; i < val; i++)
  {
      M5.dis.drawpix(i, desiredColor);
  }
}

In the next post in this series we use the Grove IR Remote module to reverse engineer a DVD remote! See you then, intrepid reader - same blog time, same blog channel!

Click here to go to the table of contents for this series.

No comments:

Post a Comment