Skip to content

Instantly share code, notes, and snippets.

@sonnny
Last active January 12, 2025 17:18
Show Gist options
  • Select an option

  • Save sonnny/d6cc2a78131f3afdc6dd9d5cf34b2372 to your computer and use it in GitHub Desktop.

Select an option

Save sonnny/d6cc2a78131f3afdc6dd9d5cf34b2372 to your computer and use it in GitHub Desktop.
pico touch pad demo
/*
touch demo for pico rp2040
cmake_minimum_required(VERSION 3.13)
set(PICO_SDK_PATH "/home/sonny/pico/pico-sdk")
set(PICOTOOL_FETCH_FROM_GIT_PATH "/home/sonny/pico/picotool")
set(PICO_BOARD waveshare_rp2040_zero CACHE STRING "Board type")
include(/home/sonny/pico/pico-sdk/external/pico_sdk_import.cmake)
project(main C CXX ASM)
pico_sdk_init()
add_executable(main main.c)
pico_enable_stdio_usb(main 1)
#pico_generate_pio_header(main ${CMAKE_CURRENT_LIST_DIR}/ws2812.pio) #use this line if you have pio
target_link_libraries(main pico_stdlib hardware_pio hardware_clocks hardware_gpio)
target_include_directories(main PUBLIC ${CMAKE_CURRENT_LIST_DIR}) #needed for usb printf
pico_add_extra_outputs(main)
press boot then press reset momentarily
sudo cp blink.uf2 /media/sonny/RPI-RP2
code from https://github.com/JeremySCook/picotouch/blob/main/arduino/capsensetest/capsensetest.ino
wire 1 megaohm resistor in series from gpio2 to ground
touch wire between gpio2 and resistor to activate touch
*/
#include <stdio.h>
#include <stdlib.h>
#include "pico/stdlib.h"
int raw=0;
int threshold=200; //adjust this for your own needs decrease or increase
//function readRaw() returns raw value
int readRaw(){
int temp=0;
for(int i=0; i<5; i++){
gpio_set_dir(2,1);
gpio_put(2,1);
sleep_us(10);
gpio_set_dir(2,0);
while(gpio_get(2) && temp < 10000){temp++;}
} return temp;}
int main(){
stdio_init_all();
sleep_ms(2000);
printf("starting...\n");
gpio_init(2); gpio_set_dir(2,1);
int baseline=readRaw(); //read baseline
for(;;){
raw=readRaw();
if(raw>baseline+threshold)printf("touched\n");//adjust 200 to suit your need up or down
else printf("not touched\n");
sleep_ms(100);}return 0;}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment