How to address pins on Circuit Express with Arduino

Play with it! Please tell us which board you're using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
DJGEENEN
 
Posts: 10
Joined: Thu Nov 17, 2022 9:53 pm

How to address pins on Circuit Express with Arduino

Post by DJGEENEN »

Hello, I am not much of a programmer and I am having difficulty finding information on using the Arduino IDE on the Circuit Playground Express. I can get an external LED to illuminate and blink but when I try to control the external LED with the left button, I cannot find out how to address the pin. I am using pin A3 at the moment. The Circuit Playground library does not allow me to define "ledPin" or "10" as a name for the pin I want to use. I have used some Arduino in the past and don't want to start completely over with MakeCode. Thanks for any help you can provide.

User avatar
dastels
 
Posts: 15653
Joined: Tue Oct 20, 2015 3:22 pm

Re: How to address pins on Circuit Express with Arduino

Post by dastels »

Have you looked at the tutorial guide? https://learn.adafruit.com/adafruit-cir ... nd-express

Dave

User avatar
DJGEENEN
 
Posts: 10
Joined: Thu Nov 17, 2022 9:53 pm

Re: How to address pins on Circuit Express with Arduino

Post by DJGEENEN »

Yes, I have looked at that, and everything other piece of information I can find. They all talk about Arduino but stop short of of supplying any details about how to address anything off the board when using the Arduino IDE. If anyone knows of a source of Arduino applications on the Circuit Playground Express I would be very happy to hear about it.

User avatar
dastels
 
Posts: 15653
Joined: Tue Oct 20, 2015 3:22 pm

Re: How to address pins on Circuit Express with Arduino

Post by dastels »

Everythign you need is on the pinout diagram at https://learn.adafruit.com/adafruit-cir ... ss/pinouts. For Arduino you want the "IDE" pin labels.

Dave

User avatar
DJGEENEN
 
Posts: 10
Joined: Thu Nov 17, 2022 9:53 pm

Re: How to address pins on Circuit Express with Arduino

Post by DJGEENEN »

"Everything you need to know ... "
It doesn't seem to be working out that way.
Here is a sketch that works great on the CPE , depends on the CPE library, and turns on the onboard LED, No.13 :

#include <Adafruit_Circuit_Playground.h> 
void setup() {
  // Initialize the circuit playground
  CircuitPlayground.begin();
}
void loop() {
  // If the left button is pressed....
  if (CircuitPlayground.leftButton()) {
      CircuitPlayground.redLED(HIGH);  // LED on
  } else {
      CircuitPlayground.redLED(LOW);   // LED off
  }
}

I have been trying to get this to work with an external LED and I have not been able to address pin A3 in a way that the library accepts, or that works. Looking for help.

CircuitPlayground.A3(HIGH);   // LED on // this way does not work
CircuitPlayground.ledPin(HIGH);  // LED on // the library does not accept "ledPin" even though it has been
defined as A3

 

User avatar
dastels
 
Posts: 15653
Joined: Tue Oct 20, 2015 3:22 pm

Re: How to address pins on Circuit Express with Arduino

Post by dastels »

The CircuitPlayground library deals with the onboard sensors and such. To use the broken out pins, the core Arduino library gives you everything needed.

In setup, configure digital pin 10 (aka A3) as an output:

Code: Select all

pinMode(10, OUTPUT);
then to use it with:

Code: Select all

digitalWrite(10, HIGH);
and

Code: Select all

digitalWrite(10, LOW);
Dave

User avatar
DJGEENEN
 
Posts: 10
Joined: Thu Nov 17, 2022 9:53 pm

Re: How to address pins on Circuit Express with Arduino

Post by DJGEENEN »

Yes, thanks. That seems to be the way forward.
Incidentally, I appreciate the conversation. Thanks for the information provided and for your time.

In case someone else out there is trying to figure these things out :

(Continuing from previous post : )

CircuitPlayground.10(HIGH); // LED on // this way, using “10” does not work
nor does “D10” or “#10”
CircuitPlayground.ledPin(HIGH); // LED on // the library does not accept "ledPin" even
though it has been defined as 10, D10, or #10.

The goal here is to control external devices from the Circuit Express. So far I have not been successful in using CPE library functions to address external devices.

Using standard Arduino coding without dependence on the library, I can get an external LED to blink on and off. The positive leg of a standard LED is connectd to Circuit Express pin A3 (aka 10) and the negative leg of the LED is connected to a 220R resistor which is connected to a ground on the Circuit Express.

const int ledPin = 10;       // choose the pin A3 for the LED      
                                                    // note: the program also works if the term “A3” is used
void setup() {
  pinMode(ledPin, OUTPUT);                            // declare LED as output
 }
void loop() {
  digitalWrite(ledPin, HIGH);    // LED on
  delay(1000);  
  digitalWrite(ledPin, LOW);   // LED off
delay(1000);
}

I can also get the left button on the Circuit Express to turn the onboard LED, number 13, on and off (see previous post for coding, using the CPE library functions). Each program works well on its own but when I put the two together in the same program, it will compile and upload, but nothing will function.
So I can write standard Arduino code, without library functions, to turn the external LED on and off with the left mouse button. Although, often it is only necessary to touch the button to get it to function, as it is operating in a capacitance, ”touch” mode.

const int ledPin = 10;                         // choose the pin 10 (aka A3) for the LED
const int inputPin = 4;                       // choose pin D4 (aka 4) for the left button
void setup() {
  pinMode(ledPin, OUTPUT);                     // declare LED as output
  pinMode(inputPin, INPUT);                   // declare left button as input
 }
void loop() {
  int val = digitalRead(inputPin);
  if (val == HIGH)
  {
  digitalWrite(ledPin, HIGH);   // LED on
  } else {
  digitalWrite(ledPin, LOW);   // LED off
  }
}

All good.
But now I would like to incorporate some of the Circuit Express's wonderful onboard features. The ring of neopixels in particular. The following library dependent code lights up the ring in blue.

#include <Adafruit_CircuitPlayground.h>
// light one pixel at a time, and light up the whole ring
int pixeln = 0;
void setup() {
  CircuitPlayground.begin();
}
void loop() {
  //Blue
  for (int pixeln = 0; pixeln < 11; pixeln++)
  CircuitPlayground.setPixelColor(pixeln,   0, 0,255);
 }

When this library dependent snippet is added to the previous standard Arduino code snippet, the neopixel ring lights up, but the standard Arduino code does not function.

With my limited knowledge, I am not able to simultaneously use the onboard, library dependent features of the Circuit Express and control external devices with standard Arduino coding.

If anyone has any helpful suggestions, I will be very appreciative.

User avatar
dastels
 
Posts: 15653
Joined: Tue Oct 20, 2015 3:22 pm

Re: How to address pins on Circuit Express with Arduino

Post by dastels »

Looks like you are doing well. This series of guides might help: https://learn.adafruit.com/lesson-0-getting-started.

Now...
When this library dependent snippet is added to the previous standard Arduino code snippet, the neopixel ring lights up, but the standard Arduino code does not function.
Can you post the code? There shouldn't be any problem.

Dave

User avatar
DJGEENEN
 
Posts: 10
Joined: Thu Nov 17, 2022 9:53 pm

Re: How to address pins on Circuit Express with Arduino

Post by DJGEENEN »

Here is the code the combines the neopixel ring library function and standard Arduino coding. It compiles and uploads but only the neopixel ring functions. The standard Arduino code for pushing the left button to light an external LED is not functioning, although when used by itself, when not combined with a library function, it does function.

Code: Select all

/*
Runs on Circuit Playground Express
Blue neopixel ring lights but button no longer lights external LED
LED 10 will not light when left button is touched or pressed
*/

#include <Adafruit_Circuit_Playground.h>

const int ledPin = 10;                               // choose the pin 10(A3) for the LED      
                                                                                                     
const int inputPin = 4;                             // choose the pin for the LED

void setup() {
   pinMode(ledPin, OUTPUT);                            // declare LED as output
  pinMode(inputPin, INPUT);                           // declare left button as input
   // Initialize the circuit playground
  CircuitPlayground.begin();
 }

void loop() {
  //Blue neopixel ring
  for (int pixeln = 0; pixeln < 11; pixeln++)
  CircuitPlayground.setPixelColor(pixeln,   0, 0,255);

    // If the left button is pressed....
  int val = digitalRead(inputPin);
  if (val == HIGH)
  {
  digitalWrite(ledPin, HIGH);  // LED on
  } else {
  digitalWrite(ledPin, LOW);   // LED off
  }
}
Last edited by dastels on Sat Nov 19, 2022 4:26 pm, edited 1 time in total.
Reason: Add code tag

User avatar
DJGEENEN
 
Posts: 10
Joined: Thu Nov 17, 2022 9:53 pm

Re: How to address pins on Circuit Express with Arduino

Post by DJGEENEN »

The comment for this line in the coding above is incorrect, it should say "left button", not "LED"

const int inputPin = 4; // choose the pin for the the left button

User avatar
dastels
 
Posts: 15653
Joined: Tue Oct 20, 2015 3:22 pm

Re: How to address pins on Circuit Express with Arduino

Post by dastels »

You're not setting a pulldown on the button input, so its behavior when unpressed in indeterminate since it's floating. I.e.

Code: Select all

pinMode(inputPin, INPUT_PULLDOWN);
If you are using the Circuitplayground library, why not use the buttons through that. All the details are taken care for you.

Dave

User avatar
DJGEENEN
 
Posts: 10
Joined: Thu Nov 17, 2022 9:53 pm

Re: How to address pins on Circuit Express with Arduino

Post by DJGEENEN »

Thanks for the suggestions. I did not understand the idea of “INPUT_PULLDOWN” and did some reading. Now I think I understand that it means when the button is not pushed, the circuit is not connected, and there is no voltage at all, so the Circuit Express cannot tell if the voltage is HIGH or LOW, since there is no circuit to evaluate, i.e. the voltage is “indeterminate” or “floating.”

I added the INPUT_PULLDOWN instruction to the code but it did not change the performance of the program. The blue neopixel ring lights but the onboard button does not turn the external LED on. The code follows:

Code: Select all

/*
Runs on Circuit Playground Express
Blue neopixel ring lights but button no longer lights external lED
LED 10 will not light when left button is touched or pressed
*/

#include <Adafruit_Circuit_Playground.h>

const int ledPin = 10;                     // choose the pin 10(A3) for the LED      
const int inputPin = 4;                    // choose the pin for left button

void setup() {

  pinMode(ledPin, OUTPUT);                           // declare LED as output
  pinMode(inputPin, INPUT);                          // declare left button as input
  pinMode(inputPin, INPUT_PULLDOWN);       // when left button not pressed, and circuit open, define as LOW
   
  // Initialize the circuit playground
  CircuitPlayground.begin();
 }

void loop() {
  //Blue neopixel ring
  for (int pixeln = 0; pixeln < 11; pixeln++)
  CircuitPlayground.setPixelColor(pixeln,   0, 0,255);

  // If the left button is pressed....
  int val = digitalRead(inputPin);
  if (val == HIGH)
  {
  digitalWrite(ledPin, HIGH);  // LED on
  } else {
  digitalWrite(ledPin, LOW);   // LED off
  }
}
As far as using the library functions for getting the left button to turn an external LED on and off, I get back to my original problem of how to address the pins in “library language.”

Here is an early attempt and failure :

Code: Select all

/*
On Circuit Playground Express)
Produces error
*/

#include <Adafruit_Circuit_Playground.h>

const int ledPin = 10;                         // choose the pin A3 (aka 10) for the LED     

void setup() {
  pinMode(ledPin, OUTPUT);              // declare LED as output

   // Initialize the circuit playground
  CircuitPlayground.begin();
 }

void loop() {
  // If the left button is pressed....
  if (CircuitPlayground.leftButton()) {
  CircuitPlayground.10(HIGH);   // LED on
} else {
  CircuitPlayground.10(LOW);    // LED off
  }   
}
Here is another attempt and failure :

Code: Select all

/*
Compiles and uploads on Circuit Playground Express
But does not run or do anything
*/

#include <Adafruit_Circuit_Playground.h>

const int ledPin = 10;                              // choose the pin A3 for the LED       
                                                    
void setup() {
  pinMode(ledPin, OUTPUT);                    // declare LED as output
    
// Initialize the circuit playground
  CircuitPlayground.begin();
 }

void loop() {
    // If the left button is pressed....
  if (CircuitPlayground.leftButton()) {
    digitalWrite(ledPin, HIGH);  // LED on
} else {
  digitalWrite(ledPin, LOW);   // LED off
  }   
}
So it can be seen that I have no idea how to use the “ library language” to connect to external devices.

Also Dave
Last edited by dastels on Sun Nov 20, 2022 10:32 am, edited 1 time in total.

User avatar
dastels
 
Posts: 15653
Joined: Tue Oct 20, 2015 3:22 pm

Re: How to address pins on Circuit Express with Arduino

Post by dastels »

You don't need

Code: Select all

pinMode(inputPin, INPUT);
as well.

What do you mean by "library language"?

I'm trying to find your problem. It's not your code. It's something about using D10. If you change ledPin to 13 it works fine with the little red LED next to the USB connector.

Dave

User avatar
dastels
 
Posts: 15653
Joined: Tue Oct 20, 2015 3:22 pm

Re: How to address pins on Circuit Express with Arduino

Post by dastels »

Doah! My morning coffee is just starting to take effect.

The problem is that you initialize CircuitPlaygound AFTER you set up the digital input/output pins. CircuitPlayground makes A3/D10 a capacitive touch input. Move the

Code: Select all

CircuitPlayground.begin();
to before

Code: Select all

pinMode(ledPin, OUTPUT); 
pinMode(inputPin, INPUT_PULLDOWN);
Dave

User avatar
DJGEENEN
 
Posts: 10
Joined: Thu Nov 17, 2022 9:53 pm

Re: How to address pins on Circuit Express with Arduino

Post by DJGEENEN »

That did it! Moving the library call to the front of “setup” has fixed the problem. It now runs beautifully. Thank you. That should have dawned on me but probably never would have.

Working code follows :

Code: Select all

/*
ButtonLight-MixPullDown-may21a.ino
Runs on Circuit Playground Express
Blue neopixel ring lights and external LED on pin 10 lights when left button pushed
*/

#include <Adafruit_Circuit_Playground.h>

const int ledPin = 10;                     // choose the pin 10(A3) for the LED     
const int inputPin = 4;                   // identify the pin for left button

void setup() {
 // Initialize the circuit playground
  CircuitPlayground.begin();  
  pinMode(ledPin, OUTPUT);                    // declare LED as output
  pinMode(inputPin, INPUT_PULLDOWN); // declare left button input and initially LOW 
 }

void loop() {
  //Blue neopixel ring
  for (int pixeln = 0; pixeln < 11; pixeln++)
  CircuitPlayground.setPixelColor(pixeln,   0, 0,255);

  // If the left button is pressed....
  int val = digitalRead(inputPin);
  if (val == HIGH)
  {
  digitalWrite(ledPin, HIGH);  // LED on
  } else {
  digitalWrite(ledPin, LOW);   // LED off
  }
}
What I mean by “library language” is the method of integrating library functions with standard Arduino language and external pins. For instance, here is a very simple to use library function that lights the onboard LED, Number 13, when the left button is pushed :

Code: Select all

void setup() {
 // Initialize the circuit playground
  CircuitPlayground.begin();
} 

void loop() {
 	// If the left button is pressed....
 	if (CircuitPlayground.leftButton()) {
 	CircuitPlayground.redLED(HIGH);  // LED on
 	} else {
 	CircuitPlayground.redLED(LOW);   // LED off
 	}
It has been a struggle to get that simple function transferred to one of the outgoing pins and an external device, in this case an external LED.

Code: Select all

void setup() {
 // Initialize the circuit playground
  CircuitPlayground.begin();  
  pinMode(ledPin, OUTPUT);                
  pinMode(inputPin, INPUT_PULLDOWN); 
 }

// If the left button is pressed....
  int val = digitalRead(inputPin);
  if (val == HIGH)
  {
  digitalWrite(ledPin, HIGH);  // LED on
  } else {
  digitalWrite(ledPin, LOW);   // LED off
  }
Looking back it seems simple. But it was not because of my limited knowledge and what seems to be limited availability of documentation for Arduino language and external devices on the Circuit Express. I have you to thank for getting me to a working routine. I don't think I would have gotten there on my own.

You've gotten me back on the trail and I've learned a lot. Now on to the difficult parts of the project.

Also Dave
cubea.gif
cubea.gif (21.57 KiB) Viewed 222 times
Last edited by dastels on Sun Nov 20, 2022 6:58 pm, edited 1 time in total.
Reason: Add code tags

Locked
Please be positive and constructive with your questions and comments.

Return to “Circuit Playground Classic, Circuit Playground Express, Circuit Playground Bluefruit”