Initial Release

This commit is contained in:
graham sanderson
2021-01-20 10:49:34 -06:00
commit 46078742c7
245 changed files with 21157 additions and 0 deletions

5
gpio/CMakeLists.txt Normal file
View File

@@ -0,0 +1,5 @@
if (NOT PICO_NO_HARDWARE)
add_subdirectory(dht_sensor)
add_subdirectory(hello_7segment)
add_subdirectory(hello_gpio_irq)
endif ()

View File

@@ -0,0 +1,11 @@
add_executable(dht
dht.c
)
target_link_libraries(dht pico_stdlib)
pico_add_extra_outputs(dht)
# add url via pico_set_program_url
example_auto_set_url(dht)

View File

@@ -0,0 +1,58 @@
= DHT-11, DHT-22, and AM2302 Sensors
:xrefstyle: short
The DHT sensors are fairly well known hobbyist sensors for measuring relative humidity and temperature using a capacitive humidity sensor, and a thermistor. While they are slow, one reading every ~2 seconds, they are reliable and good for basic data logging. Communication is based on a custom protocol which uses a single wire for data.
[NOTE]
======
The DHT-11 and DHT-22 sensors are the most common. They use the same protocol but have different characteristics, the DHT-22 has better accuracy, and has a larger sensor range than the DHT-11. The sensor is available from a number of retailers.
======
== Wiring information
See <<dht-wiring-diagram>> for wiring instructions.
[[dht-wiring-diagram]]
[pdfwidth=75%]
.Wiring the DHT-22 temperature sensor to Raspberry Pi Pico, and connecting Pico's UART0 to the Raspberry Pi 4.
image::pi-and-pico-uart-and-dht-sensor.png[]
NOTE: One of the pins (pin 3) on the DHT sensor will not be connected, it is not used.
You will want to place a 10 kΩ resistor between VCC and the data pin, to act as a medium-strength pull up on the data line.
Connecting UART0 of Pico to Raspberry Pi as in <<dht-wiring-diagram>> and you should see something similar to <<dht-serial-output-diagram>> in `minicom` when connected to `/dev/serial0` on the Raspberry Pi.
[[dht-serial-output-diagram]]
[pdfwidth=75%]
.Serial output over Pico's UART0 in a terminal window.
image::serial-output.png[]
Connect to `/dev/serial0` by typing,
----
$ minicom -b 115200 -o -D /dev/serial0
----
at the command line.
== List of Files
A list of files with descriptions of their function;
CMakeLists.txt:: Make file to incorporate the example in to the examples build tree.
dht.c:: The example code.
== Bill of Materials
.A list of materials required for the example
[[dht-22-bom-table]]
[cols=3]
|===
| *Item* | *Quantity* | Details
| Breadboard | 1 | generic part
| Raspberry Pi Pico | 1 | http://raspberrypi.org/
| 10 kΩ resistor | 1 | generic part
| M/M Jumper wires | 4 | generic part
| DHT-22 sensor | 1 | generic part
|===

83
gpio/dht_sensor/dht.c Normal file
View File

@@ -0,0 +1,83 @@
/**
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
*
* SPDX-License-Identifier: BSD-3-Clause
**/
#include <stdio.h>
#include <math.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
const uint LED_PIN = PICO_DEFAULT_LED_PIN;
const uint DHT_PIN = 15;
const uint MAX_TIMINGS = 85;
typedef struct {
float humidity;
float temp_celsius;
} dht_reading;
void read_from_dht(dht_reading *result);
int main() {
stdio_init_all();
gpio_init(LED_PIN);
gpio_init(DHT_PIN);
gpio_set_dir(LED_PIN, GPIO_OUT);
while (1) {
dht_reading reading;
read_from_dht(&reading);
float fahrenheit = (reading.temp_celsius * 9 / 5) + 32;
printf("Humidity = %.1f%%, Temperature = %.1fC (%.1fF)\n",
reading.humidity, reading.temp_celsius, fahrenheit);
sleep_ms(2000);
}
}
void read_from_dht(dht_reading *result) {
int data[5] = {0, 0, 0, 0, 0};
uint last = 1;
uint j = 0;
gpio_set_dir(DHT_PIN, GPIO_OUT);
gpio_put(DHT_PIN, 0);
sleep_ms(20);
gpio_set_dir(DHT_PIN, GPIO_IN);
gpio_put(LED_PIN, 1);
for (uint i = 0; i < MAX_TIMINGS; i++) {
uint count = 0;
while (gpio_get(DHT_PIN) == last) {
count++;
sleep_us(1);
if (count == 255) break;
}
last = gpio_get(DHT_PIN);
if (count == 255) break;
if ((i >= 4) && (i % 2 == 0)) {
data[j / 8] <<= 1;
if (count > 16) data[j / 8] |= 1;
j++;
}
}
gpio_put(LED_PIN, 0);
if ((j >= 40) && (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF))) {
result->humidity = (float) ((data[0] << 8) + data[1]) / 10;
if (result->humidity > 100) {
result->humidity = data[0];
}
result->temp_celsius = (float) (((data[2] & 0x7F) << 8) + data[3]) / 10;
if (result->temp_celsius > 125) {
result->temp_celsius = data[2];
}
if (data[2] & 0x80) {
result->temp_celsius = -result->temp_celsius;
}
} else {
printf("Bad data\n");
}
}

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 303 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 409 KiB

View File

@@ -0,0 +1,12 @@
add_executable(hello_7segment
hello_7segment.c
)
# Pull in our pico_stdlib which pulls in commonly used features
target_link_libraries(hello_7segment pico_stdlib)
# create map/bin/hex file etc.
pico_add_extra_outputs(hello_7segment)
# add url via pico_set_program_url
example_auto_set_url(hello_7segment)

View File

@@ -0,0 +1,50 @@
= Attaching a 7 segment LED via GPIO
This example code shows how to interface the Raspberry Pi Pico to a generic 7 segment LED device. It uses the LED to count from 0 to 9 and then repeat. If the button is pressed, then the numbers will count down instead of up.
== Wiring information
Our 7 Segment display has pins as follows.
----
--A--
F B
--G--
E C
--D--
----
By default we are allocating GPIO 2 to A, 3 to B etc.
So, connect GPIO 2 to pin A on the 7 segment LED display and so on. You will need the appropriate resistors (68 ohm should be fine) for each segment.
The LED device used here is common anode, so the anode pin is connected to the 3.3v supply, and the GPIO's need to pull low (to ground) to complete the circuit.
The pull direction of the GPIO's is specified in the code itself.
Connect the switch to connect on pressing. One side should be connected to ground, the other to GPIO 9.
[[hello_7segment_wiring]]
[pdfwidth=75%]
.Wiring Diagram for 7 segment LED.
image::hello_7segment_bb.png[]
== List of Files
CMakeLists.txt:: CMake file to incorporate the example in to the examples build tree.
hello_7segment.c:: The example code.
== Bill of Materials
.A list of materials required for the example
[[hello_7segment-bom-table]]
[cols=3]
|===
| *Item* | *Quantity* | Details
| Breadboard | 1 | generic part
| Raspberry Pi Pico | 1 | http://raspberrypi.org/
| 7 segment LED module | 1 | generic part
| 68 ohm resistor | 7 | generic part
| DIL push to make switch | 1 | generic switch
| M/M Jumper wires | 10 | generic part
|===

View File

@@ -0,0 +1,95 @@
/**
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
/*
Our 7 Segment display has pins as follows:
--A--
F B
--G--
E C
--D--
By default we are allocating GPIO 2 to A, 3 to B etc.
So, connect GOIP 2 to pin A on the 7 segment LED display etc. Don't forget
the appropriate resistors, best to use one for each segment!
Connect button so that pressing the switch connects the GPIO 9 (default) to
ground (pull down)
*/
#define FIRST_GPIO 2
#define BUTTON_GPIO (FIRST_GPIO+7)
// This array converts a number 0-9 to a bit pattern to send to the GPIO's
int bits[10] = {
0x3f, // 0
0x06, // 1
0x5b, // 2
0x4f, // 3
0x66, // 4
0x6d, // 5
0x7d, // 6
0x07, // 7
0x7f, // 8
0x67 // 9
};
// tag::hello_gpio[]
int main() {
stdio_init_all();
printf("Hello, 7segment - press button to count down!\n");
// We could use gpio_set_dir_out_masked() here
for (int gpio = FIRST_GPIO; gpio < FIRST_GPIO + 7; gpio++) {
gpio_init(gpio);
gpio_set_dir(gpio, GPIO_OUT);
// Our bitmap above has a bit set where we need an LED on, BUT, we are pulling low to light
// so invert our output
gpio_set_outover(gpio, GPIO_OVERRIDE_INVERT);
}
gpio_init(BUTTON_GPIO);
gpio_set_dir(BUTTON_GPIO, GPIO_IN);
// We are using the button to pull down to 0v when pressed, so ensure that when
// unpressed, it uses internal pull ups. Otherwise when unpressed, the input will
// be floating.
gpio_pull_up(BUTTON_GPIO);
int val = 0;
while (true) {
// Count upwards or downwards depending on button input
// We are pulling down on switch active, so invert the get to make
// a press count downwards
if (!gpio_get(BUTTON_GPIO)) {
if (val == 9) {
val = 0;
} else {
val++;
}
} else if (val == 0) {
val = 9;
} else {
val--;
}
// We are starting with GPIO 2, our bitmap starts at bit 0 so shift to start at 2.
int32_t mask = bits[val] << FIRST_GPIO;
// Set all our GPIO's in one go!
// If something else is using GPIO, we might want to use gpio_put_masked()
gpio_set_mask(mask);
sleep_ms(250);
gpio_clr_mask(mask);
}
return 0;
}
// end::hello_gpio[]

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 220 KiB

View File

@@ -0,0 +1,12 @@
add_executable(hello_gpio_irq
hello_gpio_irq.c
)
# Pull in our pico_stdlib which pulls in commonly used features
target_link_libraries(hello_gpio_irq pico_stdlib)
# create map/bin/hex file etc.
pico_add_extra_outputs(hello_gpio_irq)
# add url via pico_set_program_url
example_auto_set_url(hello_gpio_irq)

View File

@@ -0,0 +1,61 @@
/**
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
static char event_str[128];
void gpio_event_string(char *buf, uint32_t events);
void gpio_callback(uint gpio, uint32_t events) {
// Put the GPIO event(s) that just happened into event_str
// so we can print it
gpio_event_string(event_str, events);
printf("GPIO %d %s\n", gpio, event_str);
}
int main() {
stdio_init_all();
printf("Hello GPIO IRQ\n");
gpio_set_irq_enabled_with_callback(2, GPIO_IRQ_EDGE_RISE | GPIO_IRQ_EDGE_FALL, true, &gpio_callback);
// Wait forever
while (1);
return 0;
}
static const char *gpio_irq_str[] = {
"LEVEL_LOW", // 0x1
"LEVEL_HIGH", // 0x2
"EDGE_FALL", // 0x4
"EDGE_RISE" // 0x8
};
void gpio_event_string(char *buf, uint32_t events) {
for (uint i = 0; i < 4; i++) {
uint mask = (1 << i);
if (events & mask) {
// Copy this event string into the user string
const char *event_str = gpio_irq_str[i];
while (*event_str != '\0') {
*buf++ = *event_str++;
}
events &= ~mask;
// If more events add ", "
if (events) {
*buf++ = ',';
*buf++ = ' ';
}
}
}
*buf++ = '\0';
}